Capitalize first letter for words with length over 2 chars C#
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string[] parts = "university of washington".Split(' ');
for(int i = 0; i < parts.Length; i++)
{
Match match = Regex.Match(parts[i], @"\w{3,}",RegexOptions.IgnoreCase);
if (match.Success)
{
// Check for empty string.
if (!string.IsNullOrEmpty(parts[i]))
{
parts[i] = char.ToUpper(parts[i][0]) + parts[i].Substring(1);
}
}
}
string result = string.Join(" ", parts);
Console.WriteLine(result);
}
}
//input - university of washington
//out put - University of Washington
This could be tweaked for any length you need by changing/adding a length parameter on the Regex.
ps:Didn't use LINQ cause it had to run on a older version of .Net framework.
Written by Ion D. Filho
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#.net
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#