using namespace std; int main() { string s = "Somewhere down the road"; istringstream iss(s); do { string subs; iss >> subs; cout << "Substring: " << subs << endl; } while (iss); }
for word in string:
str.split()
, and then iterate through that . Example -my_string = "this is a string" for word in my_string.split(): print (word)
str.split()
, without passing any arguments splits by all whitespaces (space, multiple spaces, tab, newlines, etc).# Python3 code to demonstrate
# to extract words from string
# using split()
# initializing string
test_string = "GeeksforGeeks is a computer science portal for Geeks"
# printing original string
print ("The original string is : " + test_string)
# using split()
# to extract words from string
res = test_string.split()
# printing result
print ("\nThe words of string are")
for i in res:
print(i)
Output:
The original string is : GeeksforGeeks is a computer science portal for Geeks
The words of string are
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main(int argc, char** argv)
{
string text = "token test\tstring";
char_separator<char> sep(" \t");
tokenizer<char_separator<char>> tokens(text, sep);
for (const string& t : tokens)
{
cout << t << "." << endl;
}
}