Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Extract only numbers or characters from a set of characters (string)

0.00/5 (No votes)
27 Aug 2017 1  
Extract only numbers or characters from a set of characters (string)

Introduction

Hello, I hope that all of you guys doing well. Anyway let's focus what i am trying to say? As a programmer frequently i need to do extract only numbers from an alphanumeric set of characters (string). Even sometimes need to do exactly the opposite for example extract only characters (string) from set of an alphanumeric characters (string). So i would like to share a very very simple but very efficient and strong way to do this..

Using the code

A sample code is given below:

 private String foo 
       (
             String p_inputValue
       )
       {
           string rectVal = string.Empty;

           for (int i = 0; i < p_inputValue.Length; i++)
           {
               // validating the current character is a number or not
               if (!(Char.IsDigit(p_inputValue[i])))
                   rectVal += p_inputValue[i];
           }

           return rectVal;
        
       } // end function foo

Input: A123B333C45D66E6F7GH

output: ABCDEFGH

 

if you want to do exactly the opposite, you just need to change the "if" condition just like below:

 if (Char.IsDigit(p_inputValue[i]))

Input: A123B333C45D66E6F7GH

output:123333456667

History

  • 28th August, 2017: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here