Thread: Extracting parts of strings

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    Extracting parts of strings

    is it possible to extract parts of strings. ex:

    say you have an inputted value for a character string:

    something,joe

    Can you get a code which will find the ',' in the string and extract anything after it?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A non-STL method would be to use strchr() to find the comma, then increment the returned pointer by one.
    Code:
    p = strchr(myString, ',');
    if (p != NULL) p++;
    If it worked OK, p will now point to the second part of the string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    it says "undefined symbol 'p' so what should i put

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Is your string a c++ string or a char array?

    My example was for the latter, and therefore p should be defined as
    >char *p;
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        char s[] = "sentence,more";
        char *p;
        
        if ((p = strchr(s, ',')) != NULL)
        {
            p++;
            cout <<p <<endl;
        }
    }
    Last edited by Hammer; 09-15-2002 at 11:38 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    could i get the code to extract before the comma. Only one comma, not like what vVv just posted

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    do a board search for strtok() or look it up in your helpfiles.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    can someone please post the code to extract 'joe' comma for this expression:

    joe,something

    could someone post the code please?

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    If you had done as I suggested you would have found many code samples similar to this.....
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
      char test_msg[]="This is a test message";
      char words[10][20];   // 10 words, up to 20 chars
      char *p;
      char *delim = " ";
      int i = 0, j;
    
      p = strtok(test_msg,delim);
      while ( p ) {
        strcpy( words[i], p );
        p = strtok( NULL, delim );
        i++;
      } 
    
      printf ( "There are %d words\n", i );
      for ( j = 0 ; j < i ; j++ ){
        printf( "'%s'\n", words[j] );
      }
      return 0;
    }
    You will also know how to tailor this sort of code to your needs because you have read your helpfiles and know how strtok() works now.
    Still bemused??
    Then there's always politics!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Following Stoned_Coder:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    int main(void)
    {
    std::string a_string;
    std::string sub_string;
    int n;
    
    a_string = "I really like Joe, something or other.";
    
    std::cout << a_string;
    
    n = a_string.find("Joe,");
    
    sub_string = a_string.substr(n, 4);
    
    std::cout << "\n\n" << sub_string << std::endl << std::endl;
    
    system("PAUSE");
    return 0;
    }
    The code that the others have posted is more elegant and easier to maintain, but this is another example.

    Okay, niroopan, enough of our code. Let's see some of yours in the future and some specific questions beyond what you need to complete your assignment(s).

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by niroopan
    can someone please post the code to extract 'joe' comma for this expression:
    joe,something
    could someone post the code please?
    Another one for the pot, slightly shorter, doing the same as Stoner_Coders, but only doing the one comma. (Maybe easier to understand).
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        char buf[50] = "joe,something";
        strtok(buf, ",");
        cout <<buf <<endl;
        
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting words from strings
    By umerkk in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 09:13 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Extracting strings from a buffer
    By MoonSire in forum C Programming
    Replies: 11
    Last Post: 07-31-2006, 02:16 AM
  4. extracting word-per-word from strings
    By tygernoot in forum C++ Programming
    Replies: 4
    Last Post: 12-27-2004, 02:39 PM
  5. Extracting specific parts from a string
    By (TNT) in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2003, 06:43 PM