+ 2
Does c++ provide any built in methods to split a string into substrings?
I am searching for it to assist me in solving some code coach problems. I did use the search bar but previous answers was not much helpful to me.
13 ответов
+ 13
Simple answer:
Yes, there's a function called "strtok()" which is defined in header file <cstring>
Here's how to split:
/*Initializing array of characters (string)*/
char str[]{"This,is,some text."};
/*Takes address of str variable and searches for given constant delimiter to it (eg. Comma (",")) & will replace with NULL.*/
char *token = strtok(str, ",");
/*while token is not null starts executing given statements to while loop*/
while(token!=NULL){
/*Prints current characters stored in the token.*/
cout<<token<<endl; // prints "This" and endline for first execution.
/*Replace space " " with '\0' (null) in remain string, return NULL if no space found*/
token = strtok(NULL, " ");
}
Output:
This
is,some
text.
"Function: std::strtok(); // could be called as string "token".
syntax: char* strtok( char* str, const char* delim );
Defined in: <cstring>"
Know more about this function:
https://en.cppreference.com/w/cpp/string/byte/strtok
hope this helps. ;)
+ 10
rodwynnejones
I thought, initializing a variable as "str" in my previous answer can understandable as a string to others. Anyways, yes you are right we can initialise or store the string in char type array and also, the performance of char array is much better than string data type.
+ 6
InxaneNinja I'm not teaching about char array. I just explaining the use of *strtok()* function which takes *char array* only as argument.
+ 3
I found this, which is interesting:
https://www.techiedelight.com/split-string-cpp-using-delimiter/
+ 2
Hope this helps: https://www.tutorialspoint.com/substring-in-cplusplus
+ 2
In addition,
From string.h method
strtok(str, ' '); // by space
Or it can be done by using Regex (\\s+).. By space
Edit:
Use strtok() method...
+ 2
See if you can make use of this:-
http://www.cplusplus.com/reference/string/string/c_str/
The example they use just print the words on separate lines, but you can do anything you like in the actual loop...e.g. push it into a vector.
+ 2
Vinay_GB Please don't teach people to use char arrays in 2020. Only use them when absolutely necessary
+ 1
rodwynnejones
Ok. This link confused me..
http://www.cplusplus.com/articles/1UqpX9L8/
I don't know much about cpp. But since half hour, I was seeing this question is unanswered so I searched in net, as split method in c++, found 3 solution but this split seems easy one as it is in java... I answered that..
But my bad this is his user defined.. I know strtok, regex but not sure about their syntax. So I answered with split...
OK. Thank for the correction.. I will edit my answer...
+ 1
@Jayakrishna .
when double checked the site I normally use to "learn", I couldn't find the "spilt" , I just thought..."where is it...I need to know that one".
+ 1
Vinay_GB InxaneNinja
you can convert a C++ string to a char array (c style string).
0
@Jayakrishna
?? ....s.spit(' ') .....in c++...really.....show me?
I'm aware of strtok()...but not spilt.
0
Thank you for letting me know od these different methods. Sorry for wasting your time by not usong google search. My phone keeps hanging when i use chrome. Thank you again dear sololearners.