Thread: how to replace a substring in a string

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    24

    Question how to replace a substring in a string

    How can I replace a substring within a string? For example, if a line consists of different words separated from each other by comma, such as in:
    file,tcp,ftp,SF,5,6,3,normal.
    How can I locate the second word (identified by 1st and 2nd commas) and replace that word with a number of my choice?
    Dmitry Kashlev

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    The easiest way would be to remove the part of the string just after what you want to delete and then make the change. After you make the change, concatenate the part you removed earlier.
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main ( )
    {
        char *newVal = "12345";
        char string[1024] = "file,tcp,ftp,SF,5,6,3,normal";
        char holder[256];
        char *first_cut, *cut_end;
    
        // Hack it up
        first_cut = strchr (string, ',');
        cut_end = strchr (first_cut, ',');
        strcpy (holder, cut_end);
        strcpy (++first_cut, newVal);
        strcat (first_cut, holder);
    
        cout<<string<<endl;
    
        return 0;
    }

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Or using std::string, which is a bit clearer (for me, at least)

    Code:
    #include <iostream>
    #include <string>
    
    // good std::string documentation here http://www.cppreference.com/cppstring.html
    
    int main() {
    	typedef std::string::size_type size_type;
    	std::string test("file,tcp,ftp,SF,5,6,3,normal");
    	std::string replacement("udp");
    
    	// find returns position of comma, one more is passed that
    	size_type firstIndex = test.find(',') + 1;
    	size_type secondIndex = test.find(',', firstIndex);
    
    	// see if we found two commas
    	if (firstIndex == std::string::npos || secondIndex == std::string::npos) {
    		std::cout << test << " not valid format" << std::endl;
    		return 0;
    	}
    
    	// second parameter is length, not ending spot
    	test.replace(firstIndex, secondIndex - firstIndex, replacement);
    	
    	std::cout << test << std::endl;
    	
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    first_cut = strchr (string, ',');
    cut_end = strchr (first_cut, ',');
    strcpy (holder, cut_end);
    strcpy (++first_cut, newVal);
    strcat (first_cut, holder);
    Be sure to check that both calls to strchr succeeded or you will be dealing with NULL pointers. In such a situation strcpy will choke.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. String replace
    By guitarist809 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2008, 03:53 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM