![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 38
| Tokenizing strings |
| SterlingM is offline | |
| | #2 |
| Senior software engineer 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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:
| |
| anon is offline | |
| | #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);
}
}
|
| Elkvis is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |