Replace function

This is a discussion on Replace function within the C++ Programming forums, part of the General Programming Boards category; Is there a replace function, what I can use on strings, to replace certain characters. For example, replace all "_" ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Replace function

    Is there a replace function, what I can use on strings, to replace certain characters. For example, replace all "_" with " ".

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Yes. You can implement it yourself. Just loop through all the characters of a string and whenever one is of the type you want to replace, assign it the replacement value.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If you are using std::strings, there are a bunch of replace methods. I don't have time to find a link or post all of them but here's two:
    Code:
    string& string::replace (size_type idx, size_type len, size_type num, char c)
    string& string::replace (iterator beg, iterator end, size_type num, char c)
    //replace at most len characters starting at idx or in the range [beg, end)
    //with num occurrences of character c
    A google search might turn more up.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    here are two examples

    Code:
    EXAMPLE #1:
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string search ("Im going to change the spaces to !");
    	string::size_type pos = 0;
    	while( (pos = search.find(" ", pos+1)) != string::npos )
    	{
    		search.replace( pos , 1 , "!" );
    	}
    	cout << search << endl;
    	cin.get();
    	return 0;
    }
    
    EXAMPLE #2:
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string search ("Im going to change the spaces to !");
    	for( int i = 0; i != search.size(); i++ )
    	{
    		if( search[ i ] == ' ' )
    		{
    			search[ i ] = '!';
    		}
    	}
    	cout << search << endl;
    	cin.get();
    }

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ah sorry, I was in a hurry before and I guess I didn't read your post carefully. I don't think there's a replace function that does specifically what you want, but I guess ILV's solution would work
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The replace algorithm will do what you want in one line:
    Code:
    std::replace(str.begin(), str.end(), '_', ' ');

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    and incase your not sure what you need / how to implement

    here is an example of it

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string search ("Im_going_to_change_the_underlines_to_spaces");
    	replace( search.begin(), search.end(), '_' , ' ' );
    	cout << search << endl;
    	cin.get();
    	return 0;
    }

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    In ILV's second example, you could also make use of std::string::iterators.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yea i was going to use iterators, but i figured id leave one that
    was more easily understood, if he hasnt fimilarized himself
    with to many aspect of the language.

    here an example of that to
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string search ("Im_going_to_change_the_underlines_to_spaces");
    	string::iterator underlinefind;
    	for( underlinefind = search.begin(); underlinefind != search.end(); underlinefind++ )
    	{
    		if( *(underlinefind) == '_' )
    		{
    			*(underlinefind) = ' ';
    		}
    	}
    	cout << search << endl;
    	cin.get();
    }
    Last edited by ILoveVectors; 08-23-2005 at 08:12 PM.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    but if i want to replace "_*_" to " "

  11. #11
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you seriously need to try and write your own, i gave you 3
    wonderful examples, but because im not in my assholish mood
    yet.......

    this one doesnt do exactly what you want, but a few change
    on your part and you will have what you want.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	string search ("Im_going_to_change_the_*_underlines_to_spaces");
    	string::size_type pos = -1;
    	while( ( pos = search.find("_*_", pos+1) ) != string::npos )
    	{
    		search.erase( (search.begin() + pos), (search.begin() + (pos + 3)) );
    		search.insert( pos, "_" );
    	}
    	cout << search << endl;
    	cin.get();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21