I'm trying to split a string that can either be comma, space or semi-colon delimitted. It could also contain a space or spaces after each delimitter. For example
22222,11111,23232
OR
22222, 11111, 23232
OR
22222; 11111; 23232
OR
22222 11111 23232
Any one of these would produce an array with three values ["22222","11111","23232"]
So far I have var values = Regex.Split("22222, 11111, 23232", @"[\\s,;]+")
but this produces an array with the second and third values including the space(s) like so:
["22222"," 11111"," 23232"]
split(" ,") will split the string only where a space followed by a comma is the separator. Your string does not contain that sequence, hence it is not splitted. "my, tags are, in here". split(", ") with the splitting sequence swapped will at least split your original string in three parts, after each comma-and-space.
You do not only have to use literal strings for splitting strings into an array with the split method. You can use regex as breakpoints that match more characters for splitting a string.
To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.
Split(Char[]) method, except that Regex. Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The string is split as many times as possible. If no delimiter is found, the return value contains one element whose value is the original input string.
You have two possibilities:
Regex.Split
String.Split
In this case, you want to split your string by specific delimiters caracters. String.Split
has been created for this special purpose. This method will be faster than Regex.Split
.
char[] delimiters = new [] { ',', ';', ' ' }; // List of your delimiters
var splittedArray = myString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
You are using an @
symbol for your string, so the "\"
is being interpreted as a literal slash. So your character class is actually reading as a "\"
, an "s"
, a ","
or a ";"
. Remove the extra slash and it should work as desired:
var values = Regex.Split("22222, 11111, 23232", @"[\s,;]+")
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