Thread: Tokenizing strings

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Tokenizing strings

    Is there a class or function in c++ that is similar to the StringTokenizer class or the split() method?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    How about:

    Code:
    std::vector< std::string > Split( const std::string &string, const std::string &delim = " " )
    {
    	std::vector< std::string > result;
    	std::string substr;
    	for( unsigned int n = 0; n < string.size(); ++n )
    	{
    		if( delim.find_first_of( string[ n ] ) == std::string::npos )
    			substr.push_back( string[ n ] );
    		else if( substr.size() > 0 )
    		{
    			result.push_back( substr );
    			substr.clear();
    		}
    	}
    	if( substr.size() > 0 )
    		result.push_back( substr );
    	return result;
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Where can I find functions like this? I normally look at Strings library - C++ Reference but more often than not a function I need isn't listed and I have to ask on a forum. Also, are there wrapper classes like in Java? Something I could use to say like

    bool Character.isUpperCase(char ch)
    bool Character.isDigit(char ch)
    int Integer.parseInt(String s)

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Btw, I look at the whole reference. I just had the string library brought up atm.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    string is a wrapper

    Code:
    bool isUpperCase(char ch)
    {
      return ch >= 'A' &&  ch <= 'Z';
    }
    
    bool isDigit(char ch)
    {
      return ch >= '0' && ch <= '9';
    }
    
    int Integer.parseInt(String s) // you can use _atoi for this

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    See isupper, isdigit and boost::lexical_cast. For the latter you can also use stringstream and things like sscanf if you choose to go a more C way.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    this is the code that I use for tokenizing strings:

    Code:
    void TokenizeString(const std::string& str, char delim, std::vector<std::string>& v_string)
    {
      std::stringstream ss(str);
      std::string token;
      v_string.clear();
      while (std::getline(ss, token, delim)
      {
        v_string.push_back(token);
      }
    }
    but it assumes only 1 delimiter, and as such, may not suit your needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings and tokenizing need help
    By ksaman in forum C Programming
    Replies: 9
    Last Post: 02-04-2008, 06:15 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM