[英]c# Split string that contains letters and numbers
I need to split a string like this: 我需要这样分割一个字符串:
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"
My string always will be letter(s) than number(s), so I need to split it when letters finish. 我的字符串始终是字母而不是数字,因此我需要在字母结束时将其拆分。 I need to use the number only in this case. 我只需要在这种情况下使用该号码。
How I can do it? 我该怎么办? Any suggestion? 有什么建议吗?
Thanks. 谢谢。
Regex.Split will do it easily. Regex.Split会很容易做到。
string input = "11A";
Regex regex = new Regex("([0-9]+)(.*)");
string[] substrings = regex.Split(input);
You can use Regex 您可以使用正则表达式
var parts = Regex.Matches(yourstring, @"\D+|\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
You need to use a regular expression to do this: 您需要使用正则表达式来执行此操作:
string[] output = Regex.Matches(mystring, "[0-9]+|[^0-9]+")
.Cast<Match>()
.Select(match => match.Value)
.ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.