开发者

Using LINQ to parse the numbers from a string

开发者 https://www.devze.com 2023-01-11 06:34 出处:网络
Is it possible to write a query where we get all those characters that could be parsed into int from any given string?

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 slig开发者_开发百科ht harder: Can we get only first three numbers?


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()


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


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


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());


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); 


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);
    }


> '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 . . .
0

精彩评论

暂无评论...
验证码 换一张
取 消