如何通过 C# 中的多字符分隔符拆分字符串? [英] How do I split a string by a multi-character delimiter in C#?
本文介绍了如何通过 C# 中的多字符分隔符拆分字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想使用作为单词的分隔符拆分字符串怎么办?
What if I want to split a string using a delimiter that is a word?
例如,这是一个句子
.
我想拆分 is
并得到 This
和 a sentence
.
I want to split on is
and get This
and a sentence
.
在 Java
中,我可以发送一个字符串作为分隔符,但是我如何在 C#
中实现这一点?
In Java
, I can send in a string as a delimiter, but how do I accomplish this in C#
?
推荐答案
http://msdn.microsoft.com/en-us/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);
}
这篇关于如何通过 C# 中的多字符分隔符拆分字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文