Using LINQ to parse the numbers from a string

对着背影说爱祢 提交于 2019-12-09 15:08:38

问题


Is it possible to write a query where we get all those characters that could be parsed into int from any given string?

For example we have a string like: "$%^DDFG 6 7 23 1"

Result must be "67231"

And even slight harder: Can we get only first three numbers?


回答1:


This will give you your string

string result = new String("y0urstr1ngW1thNumb3rs".
    Where(x => Char.IsDigit(x)).ToArray());

And for the first 3 chars use .Take(3) before ToArray()




回答2:


The following should work.

var myString = "$%^DDFG 6 7 23 1";

//note that this is still an IEnumerable object and will need
// conversion to int, or whatever type you want.
var myNumber = myString.Where(a=>char.IsNumber(a)).Take(3);

It's not clear if you want 23 to be considered a single number sequence, or 2 distinct numbers. My solution above assumes you want the final result to be 672




回答3:


public static string DigitsOnly(string strRawData)
  {
     return Regex.Replace(strRawData, "[^0-9]", "");
  }



回答4:


string testString = "$%^DDFG 6 7 23 1";
string cleaned = new string(testString.ToCharArray()
    .Where(c => char.IsNumber(c)).Take(3).ToArray());

If you want to use a white list (not always numbers):

char[] acceptedChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
string cleaned = new string(testString.ToCharArray()
    .Where(c => acceptedChars.Contains(c)).Take(3).ToArray());



回答5:


How about something like this?

var yourstring = "$%^DDFG 6 7 23 1";  
var selected = yourstring.ToCharArray().Where(c=> c >= '0' && c <= '9').Take(3);
var reduced = yourstring.Where(char.IsDigit).Take(3); 



回答6:


Regex:

private int ParseInput(string input)
{
    System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"\d+");
    string valueString = string.Empty;
    foreach (System.Text.RegularExpressions.Match match in r.Matches(input))
        valueString += match.Value;
    return Convert.ToInt32(valueString);
}

And even slight harder: Can we get only first three numbers?

    private static int ParseInput(string input, int take)
    {
        System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"\d+");
        string valueString = string.Empty;
        foreach (System.Text.RegularExpressions.Match match in r.Matches(input))
            valueString += match.Value;
        valueString = valueString.Substring(0, Math.Min(valueString.Length, take));
        return Convert.ToInt32(valueString);
    }



回答7:


> 'string strRawData="12#$%33fgrt$%$5"; 
> string[] arr=Regex.Split(strRawData,"[^0-9]"); int a1 = 0; 
> foreach (string value in arr) { Console.WriteLine("line no."+a1+" ="+value); a1++; }'

Output:line no.0 =12
line no.1 =
line no.2 =
line no.3 =33
line no.4 =
line no.5 =
line no.6 =
line no.7 =
line no.8 =
line no.9 =
line no.10 =5
Press any key to continue . . .


来源:https://stackoverflow.com/questions/3480588/using-linq-to-parse-the-numbers-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!