-
January 28th, 2011, 03:39 PM
#1
string manipulation
Hi all, I'm trying to find a function that allows a string to be broken up in pieces. What I mean by that is this ...
Code:
char buffer[521]="I want this string broken down into words";
I suppose my question is this, is there a function that differentiates spacing with std::string or does it even matter at all? Thanx.
-
January 28th, 2011, 03:50 PM
#2
Re: string manipulation
One approach would be to construct an istringstream with the data, and then extract each whitespace-separated string from it sequentially.
-
January 28th, 2011, 04:01 PM
#3
Re: string manipulation
There is also MFC AfxExtractSubString function
Victor Nijegorodov
-
January 28th, 2011, 04:07 PM
#4
Re: string manipulation
..or you could try strtok or strtok_s
Last edited by laitinen; January 28th, 2011 at 04:40 PM.
-
January 28th, 2011, 04:36 PM
#5
Re: string manipulation
or boost has some extensive string manipulation algorithms.
-
January 28th, 2011, 04:38 PM
#6
Re: string manipulation
Originally Posted by laitinen
strtok is probably the most efficient option if you are working with a C-style string in an editable buffer, and you don't need the original data anymore once the tokenization is complete. Also, there's the same cautions and caveats you always have when dealing with C-style strings.
I tend to think that the efficiency win is mitigated by the safety concerns, but each case must be considered on its merits.
-
January 28th, 2011, 08:12 PM
#7
Re: string manipulation
I think strtok() will be something I'm willing to try. Thanx for the advice.
-
January 29th, 2011, 07:24 AM
#8
Re: string manipulation
Originally Posted by dellthinker
I think strtok() will be something I'm willing to try. Thanx for the advice.
You also need to know that strtok() uses a static buffer.
This means that you are limited in the ways you use this function. You have to strok "one string at a time". When you are fully done with one string, then you can safely strtok another string.
In other words, this would fail miserably:
Code:
char str1[100];
char str2[100];
strtok( str1, ....);
for (int i ... )
{
for (int j ... )
strtok( str2 ... )
//...
strtok(NULL, ... ); // this is for the str1 string
}
You can't have strtok going on with two strings intermingled like this.
Regards,
Paul McKenzie
-
January 29th, 2011, 09:32 AM
#9
Re: string manipulation
Originally Posted by Paul McKenzie
You also need to know that strtok() uses a static buffer.
This means that you are limited in the ways you use this function. You have to strok "one string at a time".
Very true, although if the OP has the need to split multiple strings, strtok_s() can handle that. (VS2005 and later).
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
January 30th, 2011, 06:51 AM
#10
Re: string manipulation
If your compiler already supports C++0x Regex library, you can use that.
Something as follows:
Code:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main(int argc, char** argv)
{
regex reg("\\s*[,;\\s]+\\s*");
while (1)
{
cout << "Enter a string to split (q=quit): ";
string str;
if (!getline(cin, str) || str == "q")
break;
const sregex_token_iterator end;
for (sregex_token_iterator iter(str.begin(), str.end(), reg, -1);
iter != end; ++iter)
{
cout << "\"" << *iter << "\"" << endl;
}
}
return 0;
}
-
January 30th, 2011, 09:00 PM
#11
Re: string manipulation
I have found a working solution...
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string str,str2,str3,str4;
size_t pos;
cout << "Enter 3 words separated by a space character " << endl;
getline(cin,str);
str2 = str.substr(0,8);
str3 = str.substr(9,8);
pos = str.find_last_of(" ");
str4 = str.substr(pos);
cout << str2 << endl;
cout << str3 << endl;
stringstream stream(str4);
while( getline(stream, str4, ' ') ){cout << str4;}
return 0;
}
Just in case I forget I can always refer to this post. And for any other people with the same problem. This will take a string with 3 sub strings and save them to 3 separate strings provided you type it like this "Orange Apple Watermelon", and given that the first 2 sub strings are 8 characters in length. Maybe one of the more advanced dev's here can come up with an even better solution.
-
January 31st, 2011, 04:26 AM
#12
Re: string manipulation
err, that's hardly tokenising on space is it? it just splits the input up at hard-coded positions.
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string str;
cout << "Enter 3 words separated by a space character " << endl;
getline(cin,str);
stringstream stream(str);
while( getline(stream, str2, ' ') )
{
cout << str2 << endl;
}
return 0;
}
and only slightly better is:
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> tokenize(string const & str)
{
vector<string> out;
string temp;
stringstream stream(str);
while( getline(stream, temp, ' ') )
{
out.push_back(temp);
}
return out;
}
void print(string const & s)
{
cout << s << endl;
}
int main ()
{
string str;
cout << "Enter 3 words separated by a space character " << endl;
getline(cin,str);
vector<string> toks = tokenize(str);
std::for_each(toks.begin(), toks.end(), print);
return 0;
}
Last edited by Amleto; January 31st, 2011 at 04:37 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|