Does c++ provide any built in methods to split a string into substrings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

3rd Feb 2020, 6:11 PM
Salman Nazeer
Salman Nazeer - avatar
13 Answers
+ 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. ;)
4th Feb 2020, 3:28 PM
Vinay_GB
Vinay_GB - avatar
+ 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.
5th Feb 2020, 7:37 AM
Vinay_GB
Vinay_GB - avatar
+ 6
InxaneNinja I'm not teaching about char array. I just explaining the use of *strtok()* function which takes *char array* only as argument.
5th Feb 2020, 12:52 AM
Vinay_GB
Vinay_GB - avatar
5th Feb 2020, 6:38 PM
Paul K Sadler
Paul K Sadler - avatar
3rd Feb 2020, 6:36 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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...
3rd Feb 2020, 6:52 PM
Jayakrishna 🇮🇳
+ 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.
3rd Feb 2020, 7:15 PM
rodwynnejones
rodwynnejones - avatar
+ 2
Vinay_GB Please don't teach people to use char arrays in 2020. Only use them when absolutely necessary
4th Feb 2020, 9:48 PM
inxanedev!
inxanedev! - avatar
+ 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...
3rd Feb 2020, 7:24 PM
Jayakrishna 🇮🇳
+ 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".
3rd Feb 2020, 7:39 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Vinay_GB InxaneNinja you can convert a C++ string to a char array (c style string).
5th Feb 2020, 7:25 AM
rodwynnejones
rodwynnejones - avatar
0
@Jayakrishna ?? ....s.spit(' ') .....in c++...really.....show me? I'm aware of strtok()...but not spilt.
3rd Feb 2020, 6:59 PM
rodwynnejones
rodwynnejones - avatar
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.
4th Feb 2020, 2:33 AM
Salman Nazeer
Salman Nazeer - avatar