Is there a function to split a string by a character like if you use that on a string: "do killing", it splits it to "do" and "killing" strings
Printable View
Is there a function to split a string by a character like if you use that on a string: "do killing", it splits it to "do" and "killing" strings
not sure if there's a library function to do that but something like this will work
That will outputCode:#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
//returns true if c is a space
bool space ( char c ){
return isspace(c);
}
//returns true if c is not a space
bool not_space ( char c ){
return !isspace(c);
}
//split function
vector<string> split ( string s ){
//i will be at the beginning of the string
string::iterator i = s.begin();
//vector of the words that we will return
vector<string> ret;
//run the loop until we reach the end
while ( i != s.end() ){
//find the first nonspace character from i
i = find_if ( i, s.end(), not_space );
//if i found a nonspace character
if ( i != s.end() ){
//j will be at the first space character
string::iterator j = i;
//find the space, if there is not, s.end() will be returned
j = find_if ( i, s.end(), space );
//push back the string delimited by [i, j)
ret.push_back ( string ( i, j ) );
//set i equal to j to look through the rest of the string
i = j;
}
}
return ret;
}
int main(){
string s = " hello world";
vector<string> vec = split(s);
cout << vec[0] << endl << vec[1];
return 0;
}
hello
world
but if you're just receiving input, either from a file or from cin, the input stream will ignore the whitespace unless you say so by using noskipws
that will work just fine also. try running it and typing something like "hey there stranger" or something, and you'll see that the output isCode:#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
while ( cin >> s ){
cout << s << endl;
}
return 0;
}
hey
there
stranger
Thanks
Code:#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
vector<string> tokens;
string s1 = ("Split Me Up! Word1 Word2 Word3");
string s2;
stringstream ss ( s1 );
while ( ss >> s2 )
{
cout << s2 << endl;
}
cin.get();
return 0;
}
Or you could use the good old strtok method which also allows you to split a string. strtok is a little more situational I think, but it should be perfect to split "do killing" into "do" and "killing"
How do I: Seperate a string into tokens (C++)
Here are my string splitting and trimming functions.Here's the output:Code:#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
string& trim_string(string &str);
string& trim_string(string &str,
const string &pre_chars,
const string &post_chars);
string split_string(string &str,
const string &delim_chars,
bool trim_space = true);
////////////////////////////////////////////////////////////////////////////////
// trim surrounding space from a string
string& trim_string(string &str) {
return trim_string(str, " \t\r\n", " \t\r\n");
}
// trim specified characters around a string
string& trim_string(string &str,
const string &pre_chars,
const string &post_chars)
{
// only do something if the string is not empty
if (str.length() > 0) {
string::size_type first = str.find_first_not_of(pre_chars);
string::size_type last = str.find_last_not_of(post_chars);
// if none of the pre_chars are located, just clear the string
if (first == string::npos)
str.erase();
// otherwise grab a substring after pre_chars and before post_chars
else
str = str.substr(first,
first != string::npos && last != string::npos
?
last - first + 1
:
string::npos);
}
return str;
}
////////////////////////////////////////////////////////////////////////////////
// this splits part of a string from str using any of delim_chars
// to do the tokenizing
string split_string(string &str,
const string &delim_chars,
bool trim_space)
{
string split;
// search for any of the delim_chars in the source string
string::size_type n = str.find_first_of(delim_chars);
// if none were found, split the entire string apart and leave
// the source empty
if (n == string::npos) {
split = str;
str.erase();
}
// otherwise separate the string apart from the source, insuring
// that we drop the delimiting character
else {
split = str.substr(0, n);
str = str.substr(n + 1);
}
// if the user desires, we trim the space around the split
// string and the source string
if (trim_space) {
trim_string(split);
trim_string(str);
}
return split;
}
////////////////////////////////////////////////////////////////////////////////
// here is an example
int main() {
string str = "Peter Griffin, Quahog, Rhode_Island";
while (str.length() > 0) {
string next = split_string(str, ", ");
cout << next << endl;
}
return 0;
}
Quote:
Originally Posted by output
How to do it with a char* variable?
Very carefully :).
Very similary but you would use strcpy and such.
Oh and lucky gigity gigity gigity alright!