Remove numbers from string regex

I need to remove numbers from strings like

Data1

Date3Last

so that i end up with

Data and DateLast


Using c# regex...
LVL 9
JonMnyAsked:
Who is Participating?
 
ddrudikCommented:

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "Data123";
          String matchpattern = @"\d+";
          String replacementpattern = @"";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern));
        }
    }
}

Open in new window

0
 
Fernando SotoRetiredCommented:
Hi JonMny;

When using the Regex.Replace method you need to assign the return value of that function to a variable or itself as shown below.

List<string> stripdigits = new List<string>();
stripdigits.Add("Data1");
stripdigits.Add("Date3Last");

for (int idx = 0; idx < stripdigits.Count; idx++)
{
    stripdigits[idx] = Regex.Replace(stripdigits[idx], @"\d+", String.Empty);
}

Fernando
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.