Normalize Name with a, an, the - CSharp System

CSharp examples for System:String Format

Description

Normalize Name with a, an, the

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;//  w ww . java2 s . co  m

public class Main{
        public static string NormalizeName(string name)
        {

            string input = name.ToLower().Replace("&", "and").Replace("+", "and");
            
            if (input.StartsWith("the "))
                input = input.Substring(4);
            
            if (input.StartsWith("an "))
                input = input.Substring(3);
            
            if (input.StartsWith("a "))
                input = input.Substring(2);
            
            if (input.EndsWith(", the"))
                input.Substring(0, input.Length - 5);
            
            if (input.EndsWith(", an"))
                input.Substring(0, input.Length - 4);
            
            if (input.EndsWith(", a"))
                input.Substring(0, input.Length - 3);
            
            return new Regex("[^0-9a-zA-Z]",
                RegexOptions.IgnoreCase).Replace(input, "");
        
        }
}

Related Tutorials