Using the string replace function

This is a discussion on Using the string replace function within the C++ Programming forums, part of the General Programming Boards category; The code: Code: std::string removecharacters(std::string strText) { std::string strUnwanted; int currLoc = 0; int StringLength = 0; char tmpChar; strUnwanted ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    Using the string replace function

    The code:
    Code:
    				std::string removecharacters(std::string strText)
    		{
    			std::string strUnwanted;
    			int currLoc = 0;
    			int StringLength = 0;
    			char tmpChar;
    
    			strUnwanted = "~`!@#$%^&*()_-+={}|[]\\:;'<>?,./\"";
    
    			StringLength = strUnwanted.length();
    	
    			for(currLoc=1;currLoc = StringLength;currLoc++)
    			{
    				tmpChar = strUnwanted.substr(currLoc, 1);
    				//const char chtemp = tmpChar????
    				strText.replace(strText.begin(),strText.end(), tmpChar, "");
    				
    			}
    
    			return strText;
    
    		}
    
    :\my stuff\c++\mfprocess\validation.h(62) : error C2664: 'class std::basic_string<char,struct std::char_traits<char>,
    class std::allocator<char> > &__thiscall 
    std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::replace
    (unsigned int,unsigned int,const char *,unsigned int)' :
     cannot convert parameter 1 from 'char *' to 'unsigned int'
            
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    The replace function needs a const where I am passing a string. The string is integral as it's part of the loop looking for different symbols to replace. What should I do?
    Last edited by FoodDude; 09-23-2005 at 08:18 AM.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    the stl find algorithm works better in this case to replace by yourself (the replace in string class wont do very good, it does other things)


    Code:
    std::string& removecharacters(std::string& strText)
    {
        std::string strUnwanted("~`!@#$%^&*()_-+={}|[]\\:;'<>?,./\"");
    
        int StringLength = strUnwanted.size( );
    	for (int currLoc = 0; currLoc < StringLength; currLoc++) {
            char chtemp = strUnwanted[currLoc];
           
            std::string::iterator ci = std::find(strText.begin( ), strText.end( ), chtemp);
            while (ci != strText.end( )) {
                *ci = '\0';
                ci = std::find(ci + 1, strText.end( ), chtemp);
            }		
        }
    
        return strText;
    }
    dont read strings by value, that is expensive

    http://www.cppreference.com/ <---- check that for find algorith in <algorithm> and check what the string replace function does
    Last edited by Dark-MX; 09-23-2005 at 09:08 AM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    excellent, thanks. I'm still learning.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    The problem with the original code was that it didn't match any of the existing forms of replace. Which form were you trying to use, or were you just hoping that it would match?

    Normally you should prefer the string::find function over the generic STL find algorithm, since the string specific one might be optimized for strings.

    To accomplish this task, I would use string::find_first_of and pass strUnwanted. Save the return value and use it the next time through the loop as the second parameter to find_first_of. Inside the loop use the return value of find_first_of to erase the character (I assume you didn't mean to replace it with a null character). The string::erase function takes an index and will erase that one character.

    If you really wanted to be fancy you could use for_each or some other algorithm and a function object, but the loop I described above should be fine.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    I'll look at your idea as well. I had used replace in another area but was not using variables in that case.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    my final result
    Code:
    std::string& Sremovecharacters(std::string& strText)
    {
        int iPos = 0;
        std::string strUnwanted("~`!@#$%^&*()_-+={}|[]\\:;'<>?,./\"");
        int StringLength = strUnwanted.size();
    	
    	for (int currLoc = 0; currLoc < StringLength; currLoc++) {
    		iPos = strText.find_first_of (strUnwanted, 0);
            while (iPos != -1) 
    		{
                strText.erase(iPos,1);
                iPos = strText.find_first_of (strUnwanted, (iPos+1));
            }		
        }
    
        return strText;
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Well done. One thing about the find_first_of function, it finds the first instance of any of the characters in the strUnwanted string. That means you only need to go through one time. Your for loop is unnecessary because the first time through all unwanted characters will be erased and the rest of the times through th efor loop will do nothing. So try getting rid of the for loop completely and just run the block of code inside it once. You should still get the same result.

  8. #8
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    speaking of strings, does anyone know a website that has a good reference of the 'string' class?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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