5/04/2012

Replace multiple spaces in a string with special characters

The original string:   

What are   you looking    for ?

After using code:

what-are-you-looking-for?

 

Code Demo:

   1:         static void Main(string[] args)
   2:          {                        
   3:              string keyword = "What are   you   looking for ?";            
   4:              string abc = RegexReplaceMultiSpace(keyword).ToLower();            
   5:              Console.WriteLine();
   6:          }

 

   1:          public static string RegexReplaceMultiSpace(string input)
   2:          {
   3:              string space = " ";
   4:              string character = "-";
   5:              string result = Regex.Replace(input, string.Format("[{0}]+", space), character);             
   6:              result = result.Remove(result.LastIndexOf(character),1);
   7:              return result;
   8:          }

Categories:
If You Enjoyed This Post Please Take 5 Seconds To Share It.

0 comments:

Post a Comment

 
  • Followers