C#:String split返回字符串列表和分隔符列表?

在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' 

不是我知道的,但我想你可以用正则表达式来做。 只需将其编写为仅选取分隔符,然后使用Regex.Matches ,返回的集合应包含分隔符。 请参阅此处了解更多信息,包括简短样本。