I would like to split a string into a String[] using a String as a delimiter.
String delimit = "[break]";
String[] tokens = myString.Split(delimit);
But the method above only works with a char as a delimiter.
Any takers?
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
The splitlines() method splits a string into a list. The splitting is done at line breaks.
(which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\.") , or use character class [] to represent literal character(s) like so split("[.]") , or use Pattern#quote() to escape the entire string like so split(Pattern.
Like this:
mystring.Split(new string[] { delimit }, StringSplitOptions.None);
For some reason, the only overloads of Split
that take a string take it as an array, along with a StringSplitOptions
.
I have no idea why there isn't a string.Split(params string[])
overload.
I personally prefer to use something like this, since regex has that split:
public static string[] Split(this string input, string delimit)
{
return Regex.Split(input, delimit);
}
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