0

I have been trying to use replace of std::string but have been unable to do so successfully.

I have a large string in which i want to do some replacement. I know the start and end of the substring which needs to be replaced with a new one.

static void replaceString(string &input, const string &startString, const string &endString, const string &replacement) { size_t indexStart, indexEnd; indexStart = input.find(startString); indexEnd = input.find(endString); if (indexStart != xml.npos) { input.replace(indexStart, indexEnd-indexStart, replacement); } } 

At the end of this the input remains unchanged.

What am i doing wrong here?

Regards

14
  • 1
    what is xml in line if(indexStrat != xml.npos) ? Commented Nov 26, 2013 at 6:43
  • What input to the function do you have? And what if the endString can't be found? Commented Nov 26, 2013 at 6:43
  • xml was a typo. it's input. Commented Nov 26, 2013 at 6:46
  • 1
    @chingupt what is your input, expected output and observed output ? Commented Nov 26, 2013 at 6:52
  • 1
    Well, then I suggest printing out the value of input inside the if clause, immediately after replace, then working back from there. You're not showing us enough to help you. For example, it could be that your caller is providing a copy of the xml string so you're still not operating on the value you expect, or any of dozens of other trivial mistakes. Commented Nov 26, 2013 at 8:43

2 Answers 2

1

As I see it, two checks are missing:

  1. If endString is not found,
  2. If endString is found before startString, in this case, your substraction to compute the second parameter of replace() will result in a negative length which will overflow.

Also, you might want to return a boolean to check whether there was a match or not.

Try something like this instead:

static bool replaceString(string &input, const string &startString, const string &endString, const string &replacement) { size_t indexStart, indexEnd; indexStart = input.find(startString); if (indexStart == input.npos) { return false; } indexEnd = input.find(endString, indexStart); // Note the offset to start searching // after the start index if (indexEnd == input.npos) { return false; } input.replace(indexStart, indexEnd-indexStart, replacement); return true; } 

Test program:

int main (int, char**) { string s ("abcdefghijklmnopqrstuvwxyz"); string start ("gh"); string end ("pq"); string replace ("GHIJKLMNO"); bool ok = replaceString(s, start, end, replace); std::cout << "1. found? " << ok << ", result: " << s << std::endl; start = "pq"; end = "de"; ok = replaceString(s, start, end, replace); std::cout << "2. found? " << ok << ", result: " << s << std::endl; return 0; } 

Output:

1. found? true, result: abcdefGHIJKLMNOpqrstuvwxyz 2. found? false, result: abcdefGHIJKLMNOpqrstuvwxyz 
Sign up to request clarification or add additional context in comments.

Comments

0

This answer is similar to Julien-L's but I think you also need to advance indexStart so the replacement doesn't overwrite any of the found startString.

static void replaceString(std::string &input, const std::string &startString, const std::string &endString, const std::string &replacement) { size_t indexStart = input.find(startString); if (indexStart == input.npos) return; indexStart += startString.size(); size_t indexEnd = input.find(endString, indexStart); if (indexEnd == input.npos) return; input.replace(indexStart, indexEnd - indexStart, replacement); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.