大家有好的方法也可以贴出来
// 字符串去前后空格
void trim_left_right(std::string &str)
{
string str_temp = str;
string::iterator new_end = remove_if(str_temp.begin(), str_temp.end(), bind2nd(equal_to <char>(), ' '));
str_temp.erase(new_end, str_temp.end());
new_end = remove_if(str_temp.begin(), str_temp.end(), bind2nd(equal_to <char>(), '\t'));
str_temp.erase(new_end, str_temp.end());
str = str_temp;
}
下面有个牛人写的 更好:
* Trim any leading and trailing white space characters from the string.
* Note that escape sequences and quotes are not handled.
*
* @param inStr - string to trim.
* @return - string without leading or trailing white space
*/
#define STR_WHITESPACE " \t\n\r\v\f"
{
string outStr;
if (!inStr.empty())
{
string::size_type start = inStr.find_first_not_of(STR_WHITESPACE);
// If there is only white space we return an empty string
if (start != string::npos)
{
string::size_type end = inStr.find_last_not_of(STR_WHITESPACE);
outStr = inStr.substr(start, end - start + 1);
}
}
return outStr;
}
eg:
FILE *srcFile = fopen("yan","r");
if ( srcFile == NULL )
{
return -1;
}
char buf[128];
for (;fgets(buf,128,srcFile) != NULL;memset(buf,'\0',128))
{
string tmp_str = trimWhiteSpace(string(buf));
if (tmp_str.size() > 0)
shield_prefixs.push_back(tmp_str);
}
return 0;