String replacement - C #

Let's say I have a string containing numbers and other characters.

I want to reduce a string to a number.

Fe from 23232-2222-d23231 to 23232222223231

Can this be done with string.replace ()?

if not, what is the easiest and shortest way?

10x!

+2


source to share


12 replies


There are many possibilities, starting with regular expressions and processing text yourself. I would go for this:



Regex.Replace(input, @"\D+", "")

      

+15


source


Well, you get 874355876234857 answers with String.Replace and Regex.Replace, so here's a LINQ solution:

code = new String((from c in code where Char.IsDigit(c) select c).ToArray());

      



Or using extension methods:

code = new String(code.Where(c => Char.IsDigit(c)).ToArray());

      

+4


source


Yes, you can use String.replace, but it would make more sense to use a regex so that you can match a lot more criteria with much less effort.

+2


source


The best way is with user regular expressions. Example:

RegEx.Replace("23232-2222-d23231", "\\D+", "");

      

+2


source


Regular expressions, as selective, are the simplest and shortest.

I wonder if lower will be faster?

            string sample = "23232-2222-d23231";
            StringBuilder resultBuilder = new StringBuilder(sample.Length);
            char c;
            for (int i = 0; i < sample.Length; i++)
            {
                c = sample[i];
                if (c >= '0' && c <= '9')
                {
                    resultBuilder.Append(c);
                }
            }
            Console.WriteLine(resultBuilder.ToString());
            Console.ReadLine();

      

Assuming this will depend on several things, including the length of the string.

+1


source


The simplest might be to use Replace.

 string test = "23232-2222-d23231";
 string newString = test.Replace("-","").Replace("d","");

      

But using REGEX would be better, but more stringent.

0


source


It's not possible (easy) with string.Replace()

. The simplest solution is the following function / code:

public string GetDigits(string input)
{
    Regex r = new Regex("[^0-9]+");
    return r.Replace(input, "");
}

      

0


source


I would use a regular expression.

See this Regex post for numbers only

0


source


You can use a simple extension method:

    public static string OnlyDigits(this string s)
    {
        if (s == null)
            throw new ArgumentNullException("null string");

        StringBuilder sb = new StringBuilder(s.Length);
        foreach (var c in s)
        {
            if (char.IsDigit(c))
                sb.Append(c);
        }
        return sb.ToString();
    }

      

0


source


Try

string str="23232-2222-d23231";
str=Regex.Replace(str, @"\D+", String.Empty);

      

0


source


You can use LINQ:

string allDigits = new String(input.Where(c => Char.IsDigit(c)).ToArray());

      

0


source


You can use regular expression.

string str = "sdfsdf99393sdfsd";
str = Regex.Replace(str, @"[a-zA-Z _\-]", "");

      

I've used this to only return numbers in a string before.

-1


source







All Articles