views:

120

answers:

6

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?

+8  A: 

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

Alan
@Alan similar solution as mine except you were waaay faster lol :)
Kelsey
You can write the where as `.Where(char.IsNumber)` I think since it accepts a char and returns a bool - no need for the lamda.
Rup
@Rup: Cool. Learned something new.
Alan
A: 

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); 
Matthew Whited
that's kind'a hard to read.. isn't it?
Ike
I guess it could be if you don't know LINQ and lambda expressions.
Matthew Whited
+2  A: 

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

Carlos Muñoz
I don't remember who answered with this solution first. Anyway I vote here too. Besides Alan has more that 10k score.
Ike
A: 
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());
Kelsey
A: 

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);
    }
Jake
+1  A: 
public static string DigitsOnly(string strRawData)
  {
     return Regex.Replace(strRawData, "[^0-9]", "");
  }
THines01
that one nice too
Ike