Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string using C++11

What would be easiest method to split a string using c++11?

I've seen the method used by this post, but I feel that there ought to be a less verbose way of doing it using the new standard.

Edit: I would like to have a vector<string> as a result and be able to delimitate on a single character.

like image 511
Mark Avatar asked Feb 24 '12 17:02

Mark


People also ask

How do you split a string in C?

Splitting a string using strtok() in C In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter. A token is a substring extracted from the original string.

Is there a split method in C++?

Use strtok() function to split strings delim: It is a character that is used to split a string. For example, comma (,), space ( ), hyphen (-), etc. Return: It returns a pointer that references the next character tokens.

How do I split a string into letters in C++?

Just use the string as if i would be a array. In a c++ 's std::string if example equals "Hello world!" then example[0] equals 'H' , example[1] equals 'e' and so on. Variable-length arrays are non-standard until C++17, and you don't need arr anyway since std::string overloads operator[] .

How do I split a string into string?

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

How can I split a string into multiple strings in C?

How can I split a string into multiple strings separated by different delimiters in C? Use strtok () function in c and C++. while Python has split () function which tokenize the string based on delimiter. In Java there is a class StringTokenizer which also does the same thing.

What is the use of split in Java?

In Java, split () is a method in String class. // expregexp is the delimiting regular expression; // limit is the number of returned strings public String [] split (String regexp, int limit); // We can call split () without limit also public String [] split (String regexp)

Why can’t c split strings?

The C# language has several fun and interesting string methods, which are like functions for object-oriented languages. You can parse strings, replace text within a string, and a whole host of fun and useful tasks. But C was specifically mocked regarding its inability to split a string by using a tidy number of statements.

How does the split () function work in Python?

The first thing the split () function does is to obtain the length of the original string: If the split ( offset) is greater than the string’s length, the function returns 0, failure. (The function can “split” a string the same length as the original string, in which case you end up with a copy of the original string and an empty string.)


1 Answers

std::regex_token_iterator performs generic tokenization based on a regex. It may or may not be overkill for doing simple splitting on a single character, but it works and is not too verbose:

std::vector<std::string> split(const string& input, const string& regex) {     // passing -1 as the submatch index parameter performs splitting     std::regex re(regex);     std::sregex_token_iterator         first{input.begin(), input.end(), re, -1},         last;     return {first, last}; } 
like image 176
JohannesD Avatar answered Oct 15 '22 09:10

JohannesD