Replacing Text?

Replacing text in a std::string

Page 1 of 1

9 Replies - 1058 Views - Last Post: 01 March 2009 - 02:55 PM Rate Topic: -----

#1 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Replacing Text?

Posted 01 March 2009 - 11:56 AM

I want to replace all occurrences of a certain string in another string with some other string.
Example:
"This is a example \\n This is an example."
Should become:
"This is a example \n This is a example."

I think I should be using
myString.replace();

but I don't know which overload I should use and how to use it.

Can anyone point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing Text?

#2 Aureax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 03-September 08

Re: Replacing Text?

Posted 01 March 2009 - 12:15 PM

To use the replace function:

MyString = MyString.replace("false","true");


Maybe you could make a loop out of it? :)
Was This Post Helpful? 0
  • +
  • -

#3 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Replacing Text?

Posted 01 March 2009 - 12:21 PM

View PostAureax, on 1 Mar, 2009 - 01:15 PM, said:

To use the replace function:

MyString = MyString.replace("false","true");


Maybe you could make a loop out of it? :)

That doesn't work.

Error said:

e:\visual studio projects\2008\c++\lithium\lithium\lithium.cpp(567) : error C2661: 'std::basic_string<_Elem,_Traits,_Ax>::replace' : no overloaded function takes 2 arguments
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: Replacing Text?

Posted 01 March 2009 - 02:15 PM

Read the docs.
You have to find what you want to replace first:
std::string favorite_soda = "My favorite soda is Coke";
std::cout << favorite_soda << std::endl;
size_t soda_location = favorite_soda.find("Coke");
std::string new_favorite_soda("root beer");
if (soda_location != string::npos)
   favorite_soda.replace(soda_location, 
       new_favorite_soda.length(), new_favorite_soda);
std::cout << favorite_soda << std::endl;

Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Replacing Text?

Posted 01 March 2009 - 02:32 PM

View PostJackOfAllTrades, on 1 Mar, 2009 - 03:15 PM, said:

Read the docs.
You have to find what you want to replace first:
std::string favorite_soda = "My favorite soda is Coke";
std::cout << favorite_soda << std::endl;
size_t soda_location = favorite_soda.find("Coke");
std::string new_favorite_soda("root beer");
if (soda_location != string::npos)
   favorite_soda.replace(soda_location, 
       new_favorite_soda.length(), new_favorite_soda);
std::cout << favorite_soda << std::endl;

I havn't tried your solution, but it looks as though it wouldn't work if the replacement text wasn't the same size.
Was This Post Helpful? 0
  • +
  • -

#6 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Replacing Text?

Posted 01 March 2009 - 02:38 PM

You could use the erase() and insert() string methods I believe. Although the difficult part would be finding exactly where to erase and insert the strings...
example of insert and erase. look at the comma and remove spaces part for a brief walk through and practical application. :D
Was This Post Helpful? 0
  • +
  • -

#7 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Replacing Text?

Posted 01 March 2009 - 02:42 PM

I think it will be best to use erase and insert. I don't see why it would be hard to find exactly where to erase and insert. myString.find("\\n")
Was This Post Helpful? 0
  • +
  • -

#8 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Replacing Text?

Posted 01 March 2009 - 02:49 PM

That could nicely if you made your own function that incorporated the find, erase, and insert functions. Could you post the result when you get it, or maybe submit a code snippet? o_0
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: Replacing Text?

Posted 01 March 2009 - 02:49 PM

View Postprogramble, on 1 Mar, 2009 - 04:32 PM, said:

View PostJackOfAllTrades, on 1 Mar, 2009 - 03:15 PM, said:

Read the docs.
You have to find what you want to replace first:
std::string favorite_soda = "My favorite soda is Coke";
std::cout << favorite_soda << std::endl;
size_t soda_location = favorite_soda.find("Coke");
std::string new_favorite_soda("root beer");
if (soda_location != string::npos)
   favorite_soda.replace(soda_location, 
       new_favorite_soda.length(), new_favorite_soda);
std::cout << favorite_soda << std::endl;

I havn't tried your solution, but it looks as though it wouldn't work if the replacement text wasn't the same size.


Well, you should try it because it works.
Was This Post Helpful? 0
  • +
  • -

#10 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Replacing Text?

Posted 01 March 2009 - 02:55 PM

Here is the code that I am using (my solution):
int nlPos = 0;
while ((nlPos = text.find("\\n", nlPos)) != -1)
{
	text.erase(nlPos, 2);
	text.insert(nlPos, "\n");
}


That code effectively replaces user-typed newlines with actual newlines.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1