小编典典

如何在C#中使用多字符定界符分割字符串?

c#

如果我想使用一个单词的定界符分割一个字符串怎么办?

例如,This is a sentence

我想分裂is并得到Thisa sentence

在中Java,我可以发送字符串作为定界符,但是如何在其中完成C#呢?


阅读 408

收藏
2020-05-19

共1个答案

小编典典

http://msdn.microsoft.com/zh-
CN/library/system.string.split.aspx

来自文档的示例:

string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result;

// ...
result = source.Split(stringSeparators, StringSplitOptions.None);

foreach (string s in result)
{
    Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
2020-05-19