Regex: Stripping Unwanted Chararcters

by ssi 10. June 2016 07:51
public static string StripLeadingZeros(string sAlphaNumberText)
       {
           /*
            * That will trim trailing zeros if they appear after the period in a decimal number (it will always leave a single zero).
            *  It also accounts for numbers that appear at the very end.
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText,
                        @"(?<=\.\d+?)0+(?=\D|$)",
                        String.Empty);
           return sRet;
       }
 
       public static string StripWhitespace(string sAlphaNumberText)
       {
           /*
            * sStrip Whitespace
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, @"\s+""");
           return sRet;
       }
       public static string StripAllButNumbers(string sAlphaNumberText)
       {
           /*
            * strip all characters except numbers
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, @"[^\d]"String.Empty);
           return sRet;
       }
       public static string StripNumbers(string sAlphaNumberText)
       {
           /*
            * sStrip Numbers
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, @"[\d-]"string.Empty);
           return sRet;
       }
 
       public static string StripAllSpecialCharacters(string sAlphaNumberText)
       {
           /*
            * Strip All Special Characters
            */
           string sRet = "";
 
           sRet = Regex.Replace(sAlphaNumberText, "[^0-9a-zA-Z]+""");
           return sRet;
       }

Tags: , , ,

Blog | CSharp | Regex

Calendar

<<  July 2025  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

None

Development Team @ Shelbysys

We develop custom database applications for our clients. Our development tool of choice is MS Visual Studio. 

Quotations

"A woman's guess is much more accurate than a man's certainty."
Rudyard Kipling