![](/img/trans.png)
[英]How to check if string contains certain amounts of letters and numbers 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.