如果不想读整个字符串怎么办?

Ifw*_*him 1 c# asp.net int

如果我有数字和符号的组合,我该怎么办,然后我想只读取没有符号的数字.例如,我有这种类型的数字:

626120524133452_1400231752
Run Code Online (Sandbox Code Playgroud)

通过使用C#,是否有任何可能的方法不读取整个字符串,我只想读取下划线之前的数字..

626120524133452 >> like this
Run Code Online (Sandbox Code Playgroud)

我想做什么?请给一些想法的人.提前致谢.

Son*_*nül 5

你可以使用String.IndexOfString.Substring方法一样;

string s = "626120524133452_1400231752";
int index = s.IndexOf('_');
string result = s.Substring(0, index);
Console.WriteLine(result); // Print 626120524133452
Run Code Online (Sandbox Code Playgroud)


nvo*_*igt 5

字符串只是可以查询的一系列字符:

var input = "626120524133452_1400231752";

var firstNumber = new string(input.TakeWhile(Char.IsDigit).ToArray());
Run Code Online (Sandbox Code Playgroud)