C uses regular expression to realize the method of converting initials to uppercase

  • 2021-08-17 00:41:17
  • OfStack

In this paper, an example is given to describe the method of converting initials to uppercase by using regular expressions in C #. Share it for your reference, as follows:


class Program
{
 static void Main(string[] args)
 {
  // Input strings.
  const string s1 = "samuel allen";
  const string s2 = "dot net perls";
  const string s3 = "Uppercase first letters of all words in the string.";
  // Write output strings.
  Console.WriteLine(TextTools.UpperFirst(s1));
  Console.WriteLine(TextTools.UpperFirst(s2));
  Console.WriteLine(TextTools.UpperFirst(s3));
  Console.ReadKey();
 }
 }
 public static class TextTools
 {
 /// <summary>
 /// Uppercase first letters of all words in the string.
 /// </summary>
 public static string UpperFirst(string s)
 {
  return Regex.Replace(s, @"\b[a-z]\w+", delegate(Match match)
  {
  string v = match.ToString();
  return char.ToUpper(v[0]) + v.Substring(1);
  });
 }
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

I hope this article is helpful to everyone's C # programming.


Related articles: