Removing numerics(like 2,4,5...) from a string in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naveenkenexa
    New Member
    • Oct 2007
    • 3

    Removing numerics(like 2,4,5...) from a string in c#

    Can any one please suggest me how to remove numerics in a string of c#
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    scan through each character of the string.
    if the charachetr is not a numeric copy it to the end of a new string.

    Once the above cycle is completed, reference the original string to the new string, and you have your result

    cheers

    Comment

    • aliasruel
      New Member
      • Sep 2007
      • 73

      #3
      Hi,
      Try this code below... first drag and drop a button control in your webform. then copy paste this code below:
      ok enjoy!!

      private void Button1_Click(o bject sender, System.EventArg s e)
      {
      string txtInput = "ABCX123 DEFG111HIGK912" ;
      string txt;
      txt = "";
      string c;
      int P;
      string o = "1234567890 ";
      for (int i = 0; i < txtInput.Length ; i++)
      {
      c = txtInput.Substr ing(i,1);
      P = o.IndexOf(c);
      if (P < 0)
      {
      txt += c;
      }
      }
      Response.Write( txt);
      }



      regards,
      ruel sucgang














      Originally posted by naveenkenexa
      Can any one please suggest me how to remove numerics in a string of c#

      Comment

      • naveenkenexa
        New Member
        • Oct 2007
        • 3

        #4
        hi friends at last i found that its easy using regular expressions

        steps to reproduce:

        1. use 'using system.text.reg ularexpression' namespace
        2. modifiedstring = Regex.Replace(p reviousstring, "[^0-9]+", "");

        Cheers
        Naveen

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Hmm what about:
          Code:
          string demostr="abc45de8ghj";
          for(int i=0;i<10;i++)
          {
             demostr=demostr.Replace(i.ToString(),"");
          }
          //now demostr="abcdeghj"

          Comment

          Working...