[英]how do you split a string with a string in C#
I would like to split a string into a String[] using a String as a delimiter. 我想使用String作为分隔符将字符串拆分为String []。
String delimit = "[break]";
String[] tokens = myString.Split(delimit);
But the method above only works with a char as a delimiter. 但上述方法仅适用于char作为分隔符。
Any takers? 任何接受者?
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
. 出于某种原因,带有字符串的
Split
的唯一重载将其作为数组与StringSplitOptions
一起使用。
I have no idea why there isn't a string.Split(params string[])
overload. 我不知道为什么没有
string.Split(params string[])
重载。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.