c++ string替换指定字符串及替换所有子串
替换单个字符串
string fnd = "dataset"; string rep = "labels"; string buf = "d:/data/dataset/ii.jpg"; buf = buf.replace(buf.find(fnd), fnd.length(), rep);
替换所有子串
/* 函数说明:对字符串中所有指定的子串进行替换 参数: string resource_str //源字符串 string sub_str //被替换子串 string new_str //替换子串 返回值: string */ std::string subreplace(std::string resource_str, std::string sub_str, std::string new_str) { std::string dst_str = resource_str std::string::size_type pos = 0; while((pos = dst_str.find(sub_str)) != std::string::npos) //替换所有指定子串 { dst_str.replace(pos, sub_str.length(), new_str); } return dst_str; }
去掉由于window下引入的 '/r' 字符
fstream fp("val.txt"); vector<string> fn_vec; while(!fp.eof()){ string buf; getline(fp, buf); if(buf.empty()) continue; if (buf.find("\r") > 0) buf = buf.replace(buf.find("\r"), 1, ""); } fp.close();