c# 拆分包含字母和数字的字符串

c# Split string that contains letters and numbers

我需要像这样拆分字符串:

string mystring = "A2";
mystring[0] "A" 
mystring[1] "2" 

string mystring = "A11";
mystring[0] "A" 
mystring[1] "11" 

string mystring = "A111";
mystring[0] "A" 
mystring[1] "111" 

string mystring = "AB1";
mystring[0] "AB" 
mystring[1] "1" 

我的字符串总是字母而不是数字,所以我需要在字母结束时拆分它。我只需要在这种情况下使用该号码。

我该怎么做?有什么建议吗?

谢谢。

Regex.Split 会很容易做到。

string input = "11A";
Regex regex = new Regex("([0-9]+)(.*)");
string[] substrings = regex.Split(input);

您可以使用正则表达式

var parts = Regex.Matches(yourstring, @"\D+|\d+")
            .Cast<Match>()
            .Select(m => m.Value)
            .ToArray();

您需要使用正则表达式来执行此操作:

string[] output = Regex.Matches(mystring, "[0-9]+|[^0-9]+")
.Cast<Match>()
.Select(match => match.Value)
.ToArray();