在C#中是否有任何内置的方式将文本分割成单词和分隔符数组? 我想要的是:C#:字符串拆分返回字符串列表和分隔符列表?
text = "word1 + word2 - word3";
string[] words = text.Split(new char[] { '+', '-'});
//Need list '+', '-' here?
任何想法?很明显,我可以用手处理文本...... :)
在C#中是否有任何内置的方式将文本分割成单词和分隔符数组? 我想要的是:C#:字符串拆分返回字符串列表和分隔符列表?
text = "word1 + word2 - word3";
string[] words = text.Split(new char[] { '+', '-'});
//Need list '+', '-' here?
任何想法?很明显,我可以用手处理文本...... :)
使用Regex.split()的捕获括号http://msdn.microsoft.com/en-us/library/byy2946e.aspx
string input = @"07/14/2007";
string pattern = @"(-)|(/)";
foreach (string result in Regex.Split(input, pattern))
{
Console.WriteLine("'{0}'", result);
}
// In .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
// '07'
// '14'
// '2007'
//
// In .NET 2.0, the method returns an array of
// 5 elements, as follows:
// '07'
// '/'
// '14'
// '/'
// '2007'
你正在使用.Net 2.0 – Sijin 2010-06-13 11:39:52
不是我知道,但我想你可以用正则表达式来做。只需编写它只拾取您的分隔符,然后使用Regex.Matches
并且返回的集合应包含分隔符。有关更多信息,请参阅here,其中包括简短样本。
如果您编辑您的帖子显示的话阵列中的输出应该是什么您的输入例如这将是有益的。 – 2010-06-13 11:46:45
单词数组将包含Split()通常返回的内容, 例如words = {“word1”,“word2”,“word3”} 另外我正在寻找delims = {'+',' - '} – 2010-06-13 12:07:26