.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 关注
- 1317 浏览
添加回答
举报
0/150
提交
取消