将子stringreplace为另一个子stringC ++
我怎样才能replacestring中的另一个子string中的子string,我可以使用什么函数?
eg: string test = "abc def abc def"; test.replace("abc", "hij").replace("def", "klm"); //replace occurrence of abc and def with other substring
提升stringalgorithm库方式:
#include <boost/algorithm/string/replace.hpp> { // 1. string test = "abc def abc def"; boost::replace_all(test, "abc", "hij"); boost::replace_all(test, "def", "klm"); } { // 2. string test = boost::replace_all_copy ( boost::replace_all_copy<string>("abc def abc def", "abc", "hij") , "def" , "klm" ); }
在C ++中没有内置的函数来执行此操作。 如果你想用另一个replace一个子string的所有实例,你可以通过混合调用string::find
和string::replace
。 例如:
size_t index = 0; while (true) { /* Locate the substring to replace. */ index = str.find("abc", index); if (index == std::string::npos) break; /* Make the replacement. */ str.replace(index, 3, "def"); /* Advance index forward so the next iteration doesn't pick it up as well. */ index += 3; }
在这个代码的最后一行,我已经增加了index
的string被插入到string的长度。 在这个特定的例子中,用"abc"
"def"
replace"abc"
"def"
– 这实际上是不必要的。 但是,在更一般的设置中,跳过刚被replace的string是很重要的。 例如,如果要将"abc"
replace为"abcabc"
,而不跳过新replace的string段,则此代码将不断replace部分新replace的string,直到内存耗尽。 独立地,跳过这些新字符可能稍微快一些,因为这样做可以节省一些时间和精力。
希望这可以帮助!
如果replacestring的长度与要replace的string的长度不同,我认为所有的解决scheme都会失败。 (search“abc”并replace为“xxxxxx”)一般的方法可能是:
void replaceAll( string &s, const string &search, const string &replace ) { for( size_t pos = 0; ; pos += replace.length() ) { // Locate the substring to replace pos = s.find( search, pos ); if( pos == string::npos ) break; // Replace by erasing and inserting s.erase( pos, search.length() ); s.insert( pos, replace ); } }
replace子串不应该那么难。
std::string ReplaceString(std::string subject, const std::string& search, const std::string& replace) { size_t pos = 0; while((pos = subject.find(search, pos)) != std::string::npos) { subject.replace(pos, search.length(), replace); pos += replace.length(); } return subject; }
如果你需要性能,这是一个优化的函数,它修改inputstring,它不会创buildstring的副本:
void ReplaceStringInPlace(std::string& subject, const std::string& search, const std::string& replace) { size_t pos = 0; while((pos = subject.find(search, pos)) != std::string::npos) { subject.replace(pos, search.length(), replace); pos += replace.length(); } }
testing:
std::string input = "abc abc def"; std::cout << "Input string: " << input << std::endl; std::cout << "ReplaceString() return value: " << ReplaceString(input, "bc", "!!") << std::endl; std::cout << "ReplaceString() input string not changed: " << input << std::endl; ReplaceStringInPlace(input, "bc", "??"); std::cout << "ReplaceStringInPlace() input string modified: " << input << std::endl;
输出:
Input string: abc abc def ReplaceString() return value: a!! a!! def ReplaceString() input string not modified: abc abc def ReplaceStringInPlace() input string modified: a?? a?? def
在C ++ 11中,你可以使用regex_replace
:
string test = "abc def abc def"; test = regex_replace(test, regex("def"), "klm");
str.replace(str.find(str2),str2.length(),str3);
哪里
-
str
是基本string -
str2
是要查找的子string -
str3
是replace子string
using std::string; string string_replace( string src, string const& target, string const& repl) { // handle error situations/trivial cases if (target.length() == 0) { // searching for a match to the empty string will result in // an infinite loop // it might make sense to throw an exception for this case return src; } if (src.length() == 0) { return src; // nothing to match against } size_t idx = 0; for (;;) { idx = src.find( target, idx); if (idx == string::npos) break; src.replace( idx, target.length(), repl); idx += repl.length(); } return src; }
由于它不是string
类的成员,所以它不允许像你的例子那么好的语法,但是下面的代码会做同样的事情:
test = string_replace( string_replace( test, "abc", "hij"), "def", "klm")
概括rotmax的答案,这里是一个完整的解决scheme来search和replacestring中的所有实例。 如果两个子string的大小不同,则使用string :: erase和string :: insert来replace子string,否则使用更快的string:: replace。
void FindReplace(string& line, string& oldString, string& newString) { const size_t oldSize = oldString.length(); // do nothing if line is shorter than the string to find if( oldSize > line.length() ) return; const size_t newSize = newString.length(); for( size_t pos = 0; ; pos += newSize ) { // Locate the substring to replace pos = line.find( oldString, pos ); if( pos == string::npos ) return; if( oldSize == newSize ) { // if they're same size, use std::string::replace line.replace( pos, oldSize, newString ); } else { // if not same size, replace by erasing and inserting line.erase( pos, oldSize ); line.insert( pos, newString ); } } }
如果您确定string中存在所需的子string,则会将第一次出现的"abc"
replace为"hij"
test.replace( test.find("abc"), 3, "hij");
如果你在testing中没有“abc”,它会崩溃,所以小心使用它。
string & replace(string & subj, string old, string neu) { size_t uiui = subj.find(old); if (uiui != string::npos) { subj.erase(uiui, old.size()); subj.insert(uiui, neu); } return subj; }
我认为这适合您的要求与less数代码!
由@Czarek Tomczak impoved版本。
允许std::string
和std::wstring
。
template <typename charType> void ReplaceSubstring(std::basic_string<charType>& subject, const std::basic_string<charType>& search, const std::basic_string<charType>& replace) { if (search.empty()) { return; } typename std::basic_string<charType>::size_type pos = 0; while((pos = subject.find(search, pos)) != std::basic_string<charType>::npos) { subject.replace(pos, search.length(), replace); pos += replace.length(); } }
std::string replace(const std::string & in , const std::string & from , const std::string & to){ if(from.size() == 0 ) return in; std::string out = ""; std::string tmp = ""; for(int i = 0, ii = -1; i < in.size(); ++i) { // change ii if ( ii < 0 && from[0] == in[i] ) { ii = 0; tmp = from[0]; } else if( ii >= 0 && ii < from.size()-1 ) { ii ++ ; tmp = tmp + in[i]; if(from[ii] == in[i]) { } else { out = out + tmp; tmp = ""; ii = -1; } } else { out = out + in[i]; } if( tmp == from ) { out = out + to; tmp = ""; ii = -1; } } return out; };