C++ C++;查找/替换字符串
提前感谢您的帮助,如果这是一篇双重帖子,我表示歉意,但我通读了其他几个问题,没有找到我想要的答案 我正在从事一个项目,在这个项目中,我必须输入一个字符串(String1),然后在String1中找到一个特定的字符串(String2)。然后我必须用一个新字符串(String3)替换String2 希望这是有道理的。无论如何,我能够达到预期的结果,但这是情境性的。关于代码,我会在路上解释C++ C++;查找/替换字符串,c++,string,replace,find,C++,String,Replace,Find,提前感谢您的帮助,如果这是一篇双重帖子,我表示歉意,但我通读了其他几个问题,没有找到我想要的答案 我正在从事一个项目,在这个项目中,我必须输入一个字符串(String1),然后在String1中找到一个特定的字符串(String2)。然后我必须用一个新字符串(String3)替换String2 希望这是有道理的。无论如何,我能够达到预期的结果,但这是情境性的。关于代码,我会在路上解释 int main() { string string1, from, to, newString;
int main()
{
string string1, from, to, newString;
cout << "Enter string 1: ";
getline(cin, string1);
cout << "Enter string 2: ";
cin >> from;
cout << "Enter string 3: ";
cin >> to;
newString = replaceSubstring(string1, from, to);
cout << "\n" << newString;
}
string replaceSubstring(string string1, string from, string to)
{
int index, n, x = 0;
n = from.length();
while (x < string1.length() - 1)
{
index = string1.find(from, x);
string1.replace(index, n, to);
x = index + to.length() - 1;
}
return string1;
}
intmain()
{
字符串string1,from,to,newString;
不能从;
不能>到;
newString=replaceSubstring(string1,from,to);
cout当您的FIND
调用失败时,此区域的索引将不正确:
index = string1.find(string2, x);
string1.replace(index, n, string3);
检查索引的值,然后将其传递到替换当查找调用失败时,此区域的索引将不正确:
index = string1.find(string2, x);
string1.replace(index, n, string3);
在将其传递到Replace之前,检查索引的值。首先,如果函数将更改原始字符串“in-place”会更好。在这种情况下,它将看起来像一个与成员函数Replace类似的泛型函数Replace
您应在通话后检查索引是否正确
index = string1.find(string2, x);
等于std::string::npos
。否则函数将引发异常
还有这个声明
x = index + to.length() - 1;
这是错误的
应该是这样的
x = index + to.length();
例如,假设您有一个值为“a”
的字符串,并想用它来代替“ba”
。在这种情况下,如果要使用您的语句,x将等于1(x=0+2-1)。并且将指向“ba”中的“a”。函数将再次将“a”替换为“ba”,您将得到“bba”,依此类推。也就是说,循环将是无限的
我将按照以下方式编写函数
#include <iostream>
#include <string>
void replace_all( std::string &s1, const std::string &s2, const std::string &s3 )
{
for ( std::string::size_type pos = 0;
( pos = s1.find( s2, pos ) ) != std::string::npos;
pos += s3.size() )
{
s1.replace( pos, s2.size(), s3 );
}
}
int main()
{
std::string s1( "Hello world, world, world" );
std::string s2( "world" );
std::string s3( "mccdlibby" );
std::cout << s1 << std::endl;
replace_all( s1, s2, s3 );
std::cout << s1 << std::endl;
return 0;
}
首先,如果函数将更改原始字符串“in-place”会更好。在这种情况下,它将看起来像一个与成员函数replace类似的泛型函数replace
您应在通话后检查索引是否正确
index = string1.find(string2, x);
等于std::string::npos
。否则函数将引发异常
还有这个声明
x = index + to.length() - 1;
这是错误的
应该是这样的
x = index + to.length();
例如,假设您有一个值为“a”
的字符串,并想用它来代替“ba”
。在这种情况下,如果要使用您的语句,x将等于1(x=0+2-1)。并且将指向“ba”中的“a”。函数将再次将“a”替换为“ba”,您将得到“bba”,依此类推。也就是说,循环将是无限的
我将按照以下方式编写函数
#include <iostream>
#include <string>
void replace_all( std::string &s1, const std::string &s2, const std::string &s3 )
{
for ( std::string::size_type pos = 0;
( pos = s1.find( s2, pos ) ) != std::string::npos;
pos += s3.size() )
{
s1.replace( pos, s2.size(), s3 );
}
}
int main()
{
std::string s1( "Hello world, world, world" );
std::string s2( "world" );
std::string s3( "mccdlibby" );
std::cout << s1 << std::endl;
replace_all( s1, s2, s3 );
std::cout << s1 << std::endl;
return 0;
}
Find函数返回字符串x的起始索引,索引从0开始到len-1,而不是从1开始到len
int idx = string1.find(string2, x);
if(idx >= 0)
string1.replace(index, n, string3);
Find函数返回字符串x的起始索引,索引从0开始到len-1,而不是从1开始到len
int idx = string1.find(string2, x);
if(idx >= 0)
string1.replace(index, n, string3);
用有意义的变量名替换“string1”、“string2”和“string3”将更容易识别程序的行为。我做了一些小的编辑来更改变量。string2=From和string3=to。因为我正在从string2更改为string3。学习在当前使用的系统(UNIX)中使用调试程序校园里没有调试器。如果我在家,我会这样做。不幸的是,知道是否有错误的唯一方法是编译,然后尝试一次一个地处理它们。替换“string1”、“string2”和“string3”使用有意义的变量名可以更容易地识别程序的行为。我做了一些小的编辑来更改变量。String2=From和String3=to。因为我正在从String2更改为String3。学习在当前使用的系统(UNIX)中使用调试程序校园里没有调试器。如果我在家,我会这样做。不幸的是,知道是否有错误的唯一方法是编译,然后尝试一次处理一个错误。@pm100也许你可以更具体一些。失败的查找返回-1,可能不是下一次替换时使用的理想值call@grantly-我是在评论on mccdlibby说我们在做错误检查。我把你的答案提高到100%correct@pm100啊,对不起我的错误,我误解了:)@pm100也许你可以更具体一些。一个失败的查找返回-1,可能不是一个理想的值,在下一次替换中使用call@grantly-我在评论mccdlibby说我们正在进行错误检查你的回答是100%correct@pm100啊,对不起我的错误,我误解了:)在一般情况下,这是一个错误的代码。例如,如果原始字符串非常长,并且std::string::size_type等于8字节,而int等于4字节,那么您将得到未定义的行为。如果应该是idx>std::string::npo在一般情况下,这是一个错误的代码。For例如,如果原始字符串非常长,并且std::string::size_type等于8字节,而int等于4字节,则会出现未定义的行为。如果应该是idx>std::string::npost感谢您的帮助!非常感谢您的帮助!