What is C# String Split method? In C#, a string can be broken by one or more given delimiters by using the Split method. The simple way of using the Split method can be: Sourcestring.Split(‘ ‘); Where Sourcestring is the string that you want to break. The delimiter like a comma, space etc. Is specified after the Split in parenthesis.
This question already has an answer here:
- How do I iterate over the words of a string? 76 answers
If I have a std::string containing a comma-separated list of numbers, what's the simplest way to parse out the numbers and put them in an integer array?
I don't want to generalise this out into parsing anything else. Just a simple string of comma separated integer numbers such as '1,1,1,1,2,1,1,1,0'.
marked as duplicate by jww, YSC c++Oct 9 '18 at 14:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
18 Answers
Something less verbose, std and takes anything separated by a comma.
Yet another, rather different, approach: use a special locale that treats commas as white space:
To use this, you
imbue()
a stream with a locale that includes this facet. Once you've done that, you can read numbers as if the commas weren't there at all. Just for example, we'll read comma-delimited numbers from input, and write then out one-per line on standard output:The C++ String Toolkit Library (Strtk) has the following solution to your problem:
More examples can be found Here
Alternative solution using generic algorithms and Boost.Tokenizer:
It would be a good idea to check for conversion errors in
strtol()
, of course. Maybe the code may benefit from some other error checks as well.Lots of pretty terrible answers here so I'll add mine (including test program):
Nice properties:
- No dependencies (e.g. boost)
- Not an insane one-liner
- Easy to understand (I hope)
- Handles spaces perfectly fine
- Doesn't allocate splits if you don't want to, e.g. you can process them with a lambda as shown.
- Doesn't add characters one at a time - should be fast.
- If using C++17 you could change it to use a
std::stringview
and then it won't do any allocations and should be extremely fast.
Some design choices you may wish to change:
- Empty entries are not ignored.
- An empty string will call f() once.
Example inputs and outputs:
Bad input (for instance consecutive separators) will mess this up, but you did say simple.
I'm surprised no one has proposed a solution using
std::regex
yet:This function inserts all integers at the back of the input vector. You can tweak the regular expression to include negative integers, or floating point numbers, etc.
I cannot yet comment (getting started on the site) but added a more generic version of Jerry Coffin's fantastic ctype's derived class to his post.
Thanks Jerry for the super idea.
(Because it must be peer-reviewed, adding it here too temporarily)
simple structure, easily adaptable, easy maintenance.
in the end you will have a vector of strings with every element in the sentence separated by spaces. empty strings are saved as separate items.
Simple Copy/Paste function, based on the boost tokenizer.
![C Split String By Comma C Split String By Comma](/uploads/1/2/6/2/126256598/696005777.png)
This is the simplest way, which I used a lot. It works for any one-character delimiter.
Not the answer you're looking for? Browse other questions tagged c++stringparsingstlcsv or ask your own question.
Splitting a string by some delimiter is a very common task. For example, we have a comma separated list of items from a file and we want individual items in an array.
Almost all programming languages, provide function split a string by some delimiter.
In C/C++:
// using strtok() #include <string.h> int main() char str[] = 'Geeks-for-Geeks' ; // Returns first token // delimiters present in str[]. { token = strtok (NULL, '-' ); } |
Output:
In Java :
In Java, split() is a method in String class.
// using split() public class Test public static void main(String args[]) String Str = new String( 'Geeks-for-Geeks' ); // Split above string in at-most two strings System.out.println(val); System.out.println( ' ); // Splits Str into all possible tokens System.out.println(val); } |
Output:
In Python:
print line.split() |
Output:
This article is contributed by Aditya Chatterjee. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above