std::string 去首尾空格

 

大家有好的方法也可以贴出来

 

 

 

// 字符串去前后空格
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 trimWhiteSpace(const string &inStr)
{
    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;

http://blog.csdn.net/yanook/article/details/7217753

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
std::string 是 C++ 标准库中的一个常用字符串类,提供了许多操作和功能。下面是 std::string 的使用大全,包括详细的举例说明: 1. 创建和初始化: ```cpp #include <string> std::string str1; // 创建一个空的字符串 std::string str2 = "Hello"; // 使用字符串字面值初始化 std::string str3(str2); // 使用一个现有的字符串进行拷贝构造 std::string str4(5, 'X'); // 创建一个包含 5 个 'X' 的字符串 ``` 2. 获取和修改字符: ```cpp std::string str = "Hello"; char first = str[0]; // 获取第一个字符 char last = str.back(); // 获取最后一个字符 str[0] = 'J'; // 修改第一个字符为 'J' str.push_back('!'); // 在末尾添加一个字符 str.pop_back(); // 删除末尾的一个字符 // 使用迭代器遍历字符串 for (std::string::iterator it = str.begin(); it != str.end(); ++it) { std::cout << *it << " "; } ``` 3. 拼接和连接字符串: ```cpp std::string str1 = "Hello"; std::string str2 = "World"; std::string result = str1 + " " + str2; // 字符串拼接 str1 += str2; // 将 str2 连接到 str1 的末尾 result.append("!"); // 在末尾添加字符串 result.insert(5, " C++"); // 在指定位置插入字符串 result.erase(6, 4); // 删除指定位置的一段字符串 ``` 4. 查找和替换子字符串: ```cpp std::string str = "Hello, world!"; size_t found = str.find("world"); // 查找子字符串的位置 if (found != std::string::npos) { std::cout << "Substring found at position: " << found << std::endl; } str.replace(7, 5, "C++"); // 替换指定位置的子字符串 ``` 5. 获取字符串的长度和子串: ```cpp std::string str = "Hello, world!"; int length = str.length(); // 获取字符串的长度 std::string sub1 = str.substr(0, 5); // 获取从位置 0 开始的 5 个字符的子串 std::string sub2 = str.substr(7); // 获取从位置 7 开始到末尾的子串 ``` 6. 其他常用操作: ```cpp std::string str = " Hello, World! "; str = str + "C++"; // 去除首尾空格 str.erase(0, str.find_first_not_of(' ')); str.erase(str.find_last_not_of(' ') + 1); std::transform(str.begin(), str.end(), str.begin(), ::tolower); // 将字符串转换为小写 if (str.empty()) { std::cout << "String is empty" << std::endl; } ``` 这些是 std::string 的常用操作和功能,你可以根据实际需要进行进一步的学习和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值