在数字变为字母的地方拆分字符串,反之亦然



我正在寻找更好的解决方案来解决我的问题。
我有只包含数字和字母的字符串,例如"123blue321car",我想将其拆分为列表("123","蓝色","321","car"(。

我有一个非常糟糕的解决方案:遍历字符串,添加逗号,稍后使用.以逗号作为分隔符的 Split((。但我想知道它是否好?有没有更好的主意来处理它?

您可以使用正则表达式来拆分字符串,下面是一个示例:

using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(d+|D+)";
string input = @"123blue321car";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}

最新更新