std::string 去首尾空格

本文提供了一种使用C++去除字符串前后空格的方法。通过定义trimWhiteSpace函数,利用find_first_not_of和find_last_not_of查找非空白字符的位置,并截取有效部分。此方法适用于去除字符串两端的空白字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

 

 

 

// 字符串去前后空格
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.youkuaiyun.com/yanook/article/details/7217753

#include <iostream> #include <fstream> #include <istream> #include <sstream> #include <streambuf> #include <vector> #include <string> #include <stdlib.h> void printCsv(std::vector<std::string> line_data) { std::cout << line_data[0] // << atof(line_data[1].c_str()) // << atof(line_data[2].c_str()) << line_data[1] << line_data[2] << line_data[3] << std::endl; } int main() { std::ifstream csv_data("C:\\Users\\17718\\Desktop\\newtest.csv", std::ios::in); std::string line; if (!csv_data.is_open()) { std::cout << "Error: opening file fail" << std::endl; exit(1); } std::vector<std::string> words; //声明一个字符串向量 std::string word; // ------------读取数据----------------- // 读取标题行 std::getline(csv_data, line); std::istringstream sin; // 按行读取数据 while (std::getline(csv_data, line)) { // 清空vector,只存当前行的数据 words.clear(); sin.clear(); sin.str(line); while (std::getline(sin, word, ',')) //将字符串流sin中的字符读到field字符串中,以逗号为分隔符 { std::cout << word << std::endl; words.push_back(word); //将每一格中的数据逐个push } printCsv(words); } csv_data.close(); std::ofstream outFile; outFile.open("C:\\Users\\17718\\Desktop\\newtest.csv", std::ios::out | std::ios::trunc); // 写入标题行 outFile << "name" << ',' << "income" << ',' << "expenditure" << ',' << "addr" << std::endl; // ********写入两行数据********* // 写入字符串(数字) outFile << "zhangsan" << ',' << "3000" << ',' << "1200" << ',' << "中国 陕西省" << std::endl; // 写入浮点数(转为字符串) outFile << "lisi" << ',' << std::to_string(2032.1) << ',' << std::to_string(789.2) << ',' << "中国 北京市" << std::endl; outFile.close(); return 0; }
最新发布
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值