I have strings like
我的弦
AS_!SD 2453iur ks@d9304-52kasd
AS_ !SD 2453 iur ks@d9304-52kasd
I need to get the 2 frist numbres of the string:
我需要得到字符串的2个frist numbres:
for that case will be: 2453
and 9304
该病例将为:2453和9304
I don't have any delimiter in the string to try a split, and the length of the numbers and string is variable, I'm working in C# framework 4.0 in a WPF.
我在字符串中没有任何分隔符来尝试分割,而且数字和字符串的长度是可变的,我在WPF中使用c# framework 4.0。
thanks for the help, and sorry for my bad english
谢谢你的帮助,很抱歉我的英语说得不好
3 个解决方案
#1
9
This solution will take two first numbers, each can have any number of digits
这个解需要两个前数,每个都可以有任意个数
string s = "AS_!SD 2453iur ks@d9304-52kasd";
MatchCollection matches = Regex.Matches(s, @"\d+");
string[] result = matches.Cast<Match>()
.Take(2)
.Select(match => match.Value)
.ToArray();
Console.WriteLine( string.Join(Environment.NewLine, result) );
will print
将打印
2453
9304
you can parse them to int[]
by result.Select(int.Parse).ToArray();
可以通过result.Select(int. parse).ToArray()将它们解析为int[];
#2
0
You can loop chars of your string parsing them, if you got a exception thats a letter if not is a number them you must to have a list to add this two numbers, and a counter to limitate this.
你可以循环字符串的字符来解析它们,如果你有一个例外那就是一个字母如果不是数字,你必须有一个列表来添加这两个数字,还有一个计数器来限制它。
follow a pseudocode:
遵循一个伪代码:
for char in string:
if counter == 2:
stop loop
if parse gets exception
continue
else
loop again in samestring stating this point
if parse gets exception
stop loop
else add char to list
#3
0
Alternatively you can use the ASCII encoding:
您也可以使用ASCII编码:
string value = "AS_!SD 2453iur ks@d9304-52kasd";
byte zero = 48; // 0
byte nine = 57; // 9
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
byte[] asciiNumbers = asciiBytes.Where(b => b >= zero && b <= nine)
.ToArray();
char[] numbers = Encoding.ASCII.GetChars(asciiNumbers);
// OR
string numbersString = Encoding.ASCII.GetString(asciiNumbers);
//First two number from char array
int aNum = Convert.ToInt32(numbers[0]);
int bNum = Convert.ToInt32(numbers[1]);
//First two number from string
string aString = numbersString.Substring(0,2);
#1
9
This solution will take two first numbers, each can have any number of digits
这个解需要两个前数,每个都可以有任意个数
string s = "AS_!SD 2453iur ks@d9304-52kasd";
MatchCollection matches = Regex.Matches(s, @"\d+");
string[] result = matches.Cast<Match>()
.Take(2)
.Select(match => match.Value)
.ToArray();
Console.WriteLine( string.Join(Environment.NewLine, result) );
will print
将打印
2453
9304
you can parse them to int[]
by result.Select(int.Parse).ToArray();
可以通过result.Select(int. parse).ToArray()将它们解析为int[];
#2
0
You can loop chars of your string parsing them, if you got a exception thats a letter if not is a number them you must to have a list to add this two numbers, and a counter to limitate this.
你可以循环字符串的字符来解析它们,如果你有一个例外那就是一个字母如果不是数字,你必须有一个列表来添加这两个数字,还有一个计数器来限制它。
follow a pseudocode:
遵循一个伪代码:
for char in string:
if counter == 2:
stop loop
if parse gets exception
continue
else
loop again in samestring stating this point
if parse gets exception
stop loop
else add char to list
#3
0
Alternatively you can use the ASCII encoding:
您也可以使用ASCII编码:
string value = "AS_!SD 2453iur ks@d9304-52kasd";
byte zero = 48; // 0
byte nine = 57; // 9
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
byte[] asciiNumbers = asciiBytes.Where(b => b >= zero && b <= nine)
.ToArray();
char[] numbers = Encoding.ASCII.GetChars(asciiNumbers);
// OR
string numbersString = Encoding.ASCII.GetString(asciiNumbers);
//First two number from char array
int aNum = Convert.ToInt32(numbers[0]);
int bNum = Convert.ToInt32(numbers[1]);
//First two number from string
string aString = numbersString.Substring(0,2);