14

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?

2
  • 2
    The exact requirement is not entirely clear, what would happen with a string like123abc456def, do you want to return 123456, 123 and 456, just 123 or perhaps 1 and 2 and 3 etc? The best approach depends on the exact spec. Commented Aug 13, 2010 at 21:03
  • 2
    @Paul it says first three numbers. How that possibly could be more exact? Maybe you'd like me to write 300 pages of specs and some sort of a business plan with bunch of diagrams? :) Commented Aug 16, 2010 at 13:35

7 Answers 7

26

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

Sign up to request clarification or add additional context in comments.

1 Comment

I don't remember who answered with this solution first. Anyway I vote here too. Besides Alan has more that 10k score.
11

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

2 Comments

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: Cool. Learned something new.
4
public static string DigitsOnly(string strRawData)
  {
     return Regex.Replace(strRawData, "[^0-9]", "");
  }

Comments

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

Comments

1

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

1 Comment

I guess it could be if you don't know LINQ and lambda expressions.
0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.