C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2009, 04:56 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 38
Tokenizing strings

Is there a class or function in c++ that is similar to the StringTokenizer class or the split() method?
SterlingM is offline   Reply With Quote
Old 11-03-2009, 05:16 PM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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;
}
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 11-03-2009, 07:44 PM   #3
Registered User
 
Join Date: Oct 2009
Posts: 38
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)
SterlingM is offline   Reply With Quote
Old 11-03-2009, 07:44 PM   #4
Registered User
 
Join Date: Oct 2009
Posts: 38
Btw, I look at the whole reference. I just had the string library brought up atm.
SterlingM is offline   Reply With Quote
Old 11-04-2009, 07:59 AM   #5
3735928559
 
Join Date: Mar 2008
Posts: 662
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
m37h0d is offline   Reply With Quote
Old 11-04-2009, 08:23 AM   #6
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is offline   Reply With Quote
Old 11-04-2009, 10:14 AM   #7
Registered User
 
Join Date: Oct 2006
Posts: 263
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.
Elkvis is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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