2

文字列から数字ではないすべての「ジャンク」を削除するためにこれを見つけました

TextIN = " 0 . 1 ,2 ; 3 4 -5 6 ,7 ,8; 9 "

string justNumbers = new String(textIN.Where(Char.IsDigit).ToArray());

= "0123456789"

this removes all "junk" from my string leaving me just the numbers, but still how i can modify this , so i can have at least one delimiter for example a ' , ' b etween my numbers like "0,1,2,3,4,5,6,7,8,9" because i need to delimit this number so i can put them in an array of ints and work with them and there are not always just one digit numbers i may have 105 , 85692 etc.. any help please ?!

4

4 に答える 4

5

You can also convert to numeric values like this:

int[] numbers = Regex.Matches(textIN, "(-?[0-9]+)").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();

@L.B: agreed, but nthere might be negative values, too.

于 2012-04-06T11:04:01.770 に答える
1
string test = string.Join(",", textIN.Where(Char.IsDigit));
于 2012-04-06T10:50:13.110 に答える
1

n桁の数字の場合、正規表現を使用できます。

string s = String.Join(",",
                  Regex.Matches(textIN,@"\d+").Cast<Match>().Select(m=>m.Value));
于 2012-04-06T11:01:39.887 に答える
0
string justNumbers = new String(textIN.Where(Char.IsDigit).ToArray()); = "0123456789"
string[] words = justNumbers.Split(',');

文字列をコンマで区切られた数値の配列に区切ります。

于 2012-04-06T10:52:22.887 に答える