Friday, May 17, 2013

How to convert only alphabet character to uppercase or lowercase in the string

Today I got a question from my teammate how can we quickly convert those alphabet characters only in a give string.

if we want to efficiently convert find those alphabet characters and  change it to Upper or Lowercase,

use the regular expression is best approach.

first we need to define the regular expression term for the match.

[a-z] reprsents that match all lower case alphabet characters

[A-Z] will be the pattern that match all upper case alphabet.

next step
we will create a delegate that will convert the alphabet to uppercase or lowercase when the match is found

delegate(Match match)
  {
       return  match.ToString().ToUpper();
  });

the following snippet of code will demo how it works

 static void Main(string[] args)
 {
   string testString="12po78er67oiiu";
  string test = System.Text.RegularExpressions.Regex.Replace(testString, @"[a-z]"delegate(Match match)
  {
       return  match.ToString().ToUpper();
  });
}