I need to split a string separated by multiple spaces. For example:
"AAAA AAA BBBB BBB BBB CCCCCCCC"
I want to split it into these:
"AAAA AAA"
"BBBB BBB BBB"
"CCCCCCCC"
I tried with this code:
value2 = System.Text.RegularExpressions.Regex.Split(stringvalue, @"\s+");
But not success, I only want to split the string by multiple spaces, not by single space.
To split the sentences by comma, use split(). For removing surrounding spaces, use trim().
Python String split() MethodThe split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Split(char[], StringSplitOptions) Method This method is used to splits a string into substrings based on the characters in an array. You can specify whether the substrings include empty array elements. Syntax: public String[] Split(char[] separator, StringSplitOptions option);
+
means "one or more", so a single space would qualify as a separator. If you want to require more than once, use {m,n}
:
value2 = System.Text.RegularExpressions.Regex.Split( stringvalue, @"\s{2,}");
The {m,n}
expression requires the expression immediately prior to it match m
to n
times, inclusive. Only one limit is required. If the upper limit is missing, it means "m
or more repetitions".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With