C++ 尝试替换字符串中的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9053687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to replace words in a string
提问by Jlegend
I am trying to take the words from output and find any word with the letter Q in it. If the word does, it needs to be replaced by the word "bad". After that, I am trying to append each word to output2. I am having trouble doing this. The error I get when I compile is:
我试图从输出中提取单词并找到其中包含字母 Q 的任何单词。如果是这样的话,就需要用“坏”这个词来代替。之后,我试图将每个单词附加到 output2。我在做这件事时遇到了麻烦。我编译时得到的错误是:
invalid conversion from 'const char*' to 'char' [-fpermissive]
从“const char*”到“char”的无效转换[-fpermissive]
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
string manipulate(string x);
int main(int argc, char* argv[])
{
string input, temp, output, output2, test, test2;
int b;
cout << "Enter a string: ";
getline(cin, input);
istringstream iss(input);
while (iss >> test)
{
if(test.length() != 3)
{
test.append(" ", 1);
output.append(test);
}
}
istringstream iss2(output);
while (iss2 >> test2)
{
for(int i = 0; i<test2.length(); i++)
{
switch(test2[i])
{
case 'q':
test2[1]="bad";
output2.append(test2);
break;
}
}
}
cout << "Your orginal string was: " << input << endl;
cout << "Your new string is: " << output2 << endl;
cin.get();
return 0;
}
回答by LihO
There is much easier way how to do that:
有更简单的方法来做到这一点:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Your homework is bad. Really bad.");
while (s.find("bad") != string::npos)
s.replace(s.find("bad"), 3, "good");
cout << s << endl;
return 0;
}
output:
输出:
Your homework is good. Really good.
But watch out for case when the needle is a substring of the new value. In that case you might want to be shifting the index to avoid an infinite loop; example:
但要注意当针是新值的子串时的情况。在这种情况下,您可能希望移动索引以避免无限循环;例子:
string s("Your homework is good. Really good."),
needle("good"),
newVal("good to go");
size_t index = 0;
while ((index = s.find(needle, index)) != string::npos) {
s.replace(index, needle.length(), newVal);
index += newVal.length();
}
cout << s << endl;
outputs
产出
Your homework is good to go. Really good to go.
回答by hmjd
This is the cause of the compilation error:
这是编译错误的原因:
test2[1]="bad";
test2[1]
is of type char
and "bad"
is of type const char*
: this assignment is not legal.
test2[1]
is of type char
and "bad"
is of type const char*
:这个赋值是不合法的。
Use std::string::replace()
to change q
to "bad"
:
使用std::string::replace()
到的变化q
来"bad"
:
test2.replace(i, 1, "bad");
As you also only require to replace the first occurrence of 'q'
(I think this based on the logic in the for
loop) you could replace the for
loop with:
由于您还只需要替换第一次出现的'q'
(我认为这是基于for
循环中的逻辑),您可以将for
循环替换为:
size_t q_idx = test2.find('q');
if (std::string::npos != q_idx)
{
test2.replace(q_idx, 1, "bad");
output2.append(test2);
}
EDIT:
编辑:
To replace the whole word:
替换整个单词:
test2 = "bad";
Note, output2
will contain words if they contain 'q'
with the current logic. This would be one way of correcting it:
注意,output2
如果它们包含'q'
当前逻辑,将包含单词。这将是纠正它的一种方法:
output2.append(std::string::npos != test2.find('q') ? "bad" : test2);