vla*_*adr 15
见std::string::find_first_not_of.
要查找第一个非空格字符的位置(索引):
str.find_first_not_of(' ');
Run Code Online (Sandbox Code Playgroud)
要查找第一个非空白字符的位置(索引):
str.find_first_not_of(" \t\r\n");
Run Code Online (Sandbox Code Playgroud)
str.npos如果str为空或者完全由空白组成,则返回.
您可以使用find_first_not_of修剪有问题的前导空格:
str.erase(0, str.find_first_not_of(" \t\r\n"));
Run Code Online (Sandbox Code Playgroud)
如果你不想硬编码的字符数为空白(如使用的语言环境),你仍然可以使用isspace,并find_if或多或少最初建议的方式履行机构,但照顾否定isspace,如:
string::iterator it_first_nonspace = find_if(str.begin(), str.end(), not1(isspace));
// e.g. number of blank characters to skip
size_t chars_to_skip = it_first_nonspace - str.begin();
// e.g. trim leading blanks
str.erase(str.begin(), it_first_nonspace);
Run Code Online (Sandbox Code Playgroud)
我只有一个问题:你真的需要额外的空白吗?
我会在那里调用Boost.String的力量;)
std::string str1 = " hello world! ";
std::string str2 = boost::trim_left_copy(str1); // str2 == "hello world! "
Run Code Online (Sandbox Code Playgroud)
有很多操作(find,trim,replace,...),以及谓词可以在这个库,只要你需要string,酒店不提供开箱即用的操作,点击这里.此外,算法每次都有几个变量(不区分大小写并且通常复制).