.net 如何獲取一個字符串中第一個數字的位置
3 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
string input = "You 2 beautiful";
Regex regex = new Regex(@"\d");
Console.WriteLine(regex.Match(input).Index);

慕神8447489
TA貢獻1780條經驗 獲得超1個贊
1.引入VB命名空間,導入:Microsoft.VisualBasic的引用
2.可以用VB的一個函數,它自動獲取字符串中最先出現的數字。
Microsoft.VisualBasic.Conversion.Val("字符串12測試");返回12
3.然后再截取一位就OK了,然后再循環indexof函數求之。思路給你這樣說了,自己去整。

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
public static int GetNumPosition(string input) {
if (string.IsNullOrEmpty(input))
return -1;
for (int i = 0; i < input.Length; i++)
if (char.IsDigit(input[i]))
return i;
return -1;
}
- 3 回答
- 0 關注
- 1339 瀏覽
添加回答
舉報
0/150
提交
取消