10

I'm having a String like

XQ74MNT8244A

i nee to remove all the char from the string.

so the output will be like

748244

How to do this? Please help me to do this

3
  • You don't need a regex - see @Tim Robinson's answer. Commented Dec 6, 2010 at 12:39
  • You don't want LINQ answers? Can you rewrite your question, or close it and post a new one? Commented Dec 6, 2010 at 12:39
  • 1
    Your question ask for LINQ, but your comment ask for regex. What do you actually want? In this case, I think LINQ is thebest solution as Tim Robinson's solution is by far more readable than any RegExbased solution. Commented Dec 6, 2010 at 12:45

8 Answers 8

24
new string("XQ74MNT8244A".Where(char.IsDigit).ToArray()) == "748244"
Sign up to request clarification or add additional context in comments.

5 Comments

+1. Very elegant. One question here. How does Where(char.IsDigit) work in this case? I would think that you have to write Where(c => char.IsDigit(c)) here? Does the compiler do some magic in this case? An explanation would be appreciated here.
@Øyvind - no magic. Char.IsDigit is a function that takes a char and return a bool - exactly what Where expects. You don't have to wrap it in an anonymous function. (oh, and the compiler does a lot of magic anyway, like anonymous functions)
@Øyvind You can provide any delegate to the Where method; lambda syntax is only one way to obtain a delegate. If your lambda makes a direct call to some method, and the method's parameter types and return type match those of the lambda, you can provide the method name directly.
@Kobi and Tim - Thanks. I see that it should of course work this way, but I never thought about it. This might make my code a lot neater some places where I can omit the wrapper. Thanks a lot.
The value doesn't come in one string. It is indexed and separated. How to get the numbers or letters into one string?
12

Two options. Using Linq on .Net 4 (on 3.5 it is similar - it doesn't have that many overloads of all methods):

string s1 = String.Concat(str.Where(Char.IsDigit));

Or, using a regular expression:

string s2 = Regex.Replace(str, @"\D+", "");

I should add that IsDigit and \D are Unicode-aware, so it accepts quite a few digits besides 0-9, for example "542abc٣٤".
You can easily adapt them to a check between 0 and 9, or to [^0-9]+.

4 Comments

A lot can be done using LINQ but that doesn't mean it should be used for everything. +1 for suggesting RegEx option.
The use of string.concat here is nicer than the .ToArray() I was thinking of using.
@Unmesh I agree it's easy to see Linq as a golden hammer, I've been guilty of that myself on some occasions. In this case I think the Linq solution is much nicer to read than the Regex. (possibly much quicker as well)
@Andy - Thanks! Again, .Net 4 has an overload which accepts IEnumerable<char>, so it fits nicely, and probably optimized for that.
6
string value = "HTQ7899HBVxzzxx";
Console.WriteLine(new string(
     value.Where(x => (x >= '0' && x <= '9'))
     .ToArray()));

Comments

2

If you need only digits and you really want Linq try this:

youstring.ToCharArray().Where(x => char.IsDigit(x)).ToArray();

2 Comments

ToCharArray() is useless here as it implements IEnumerable
@PierrOz - That is absolutely true. However, Visual Studio 2008 Intellisense hides the IEnumerable<Char> extension methods, so it's easy to miss.
2

Using LINQ:

public string FilterString(string input)
{
    return new string(input.Where(char.IsNumber).ToArray());
}

Comments

2

Something like this?


"XQ74MNT8244A".ToCharArray().Where(x => { var i = 0; return Int32.TryParse(x.ToString(), out i); })

Comments

2
string s = "XQ74MNT8244A";
var x = new string(s.Where(c => (c >= '0' && c <= '9')).ToArray());

Comments

2

How about an extension method (and overload) that does this for you:

    public static string NumbersOnly(this string Instring)
    {
        return Instring.NumbersOnly("");
    }

    public static string NumbersOnly(this string Instring, string AlsoAllowed)
    {
        char[] aChar = Instring.ToCharArray();
        int intCount = 0;
        string strTemp = "";

        for (intCount = 0; intCount <= Instring.Length - 1; intCount++)
        {
            if (char.IsNumber(aChar[intCount]) || AlsoAllowed.IndexOf(aChar[intCount]) > -1)
            {
                strTemp = strTemp + aChar[intCount];
            }
        }

        return strTemp;
    }

The overload is so you can retain "-", "$" or "." as well, if you wish (instead of strictly numbers).

Usage:

string numsOnly = "XQ74MNT8244A".NumbersOnly();

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.