Name Capitalization Class in C# and PowerShell
I wrote a name capitalization class in PowerShell and C#. Just a simple example of how I dealt with name clean up.
using System; using System.Globalization; namespace Test { /// <summary> /// Sets any string input to capitalized. User for human names. /// </summary> public class NormName { TextInfo textinfo = new CultureInfo("en-US", false).TextInfo; private string MC(string s) { var a = s.Split('c', 2); string aFirst = this.textinfo.ToTitleCase(a[0]).Trim(); string aLast = this.textinfo.ToTitleCase(a[1]).Trim(); string n = string.Format("{0}c{1}", aFirst, aLast); return n; } private string SQ(string s) { var a = s.Split("'"); string aFirst = this.textinfo.ToTitleCase(a[0]).Trim(); string aLast = this.textinfo.ToTitleCase(a[1]).Trim(); string n = string.Format("{0}'{1}", aFirst, aLast); return n; } private string HY(string s) { var a = s.Split("-"); string aFirst = a[0].Trim(); if (aFirst.StartsWith("mc")) { aFirst = this.MC(aFirst); } else if (aFirst.Contains("'")) { aFirst = this.SQ(aFirst); } else { aFirst = this.textinfo.ToTitleCase(aFirst); } string aLast = a[1].Trim(); if (aLast.StartsWith("mc")) { aLast = this.MC(aLast); } else if (aLast.Contains("'")) { aLast = this.SQ(aLast); } else { aLast = this.textinfo.ToTitleCase(aLast); } string n = string.Format("{0}-{1}", aFirst, aLast); return n; } private string NORM(string s) { string n = textinfo.ToTitleCase(s); return n; } public string Capitalize(string sName, bool initial = false) { string clean = ""; if (!string.IsNullOrEmpty(sName)) { string prep = sName.Trim().ToLower(); if (prep.Contains("-")) { clean = this.HY(prep); } else if (prep.StartsWith("mc")) { clean = this.MC(prep); } else if (prep.Contains("'")) { clean = this.SQ(prep); } else { clean = textinfo.ToTitleCase(prep); } // Initial middle name if (initial) { clean = string.Format("{0}.", prep[0].ToString().Trim().ToUpper()); } } return clean.Trim(); } } // USAGE ... class Program { static void Main(string[] args) { NormName normname = new NormName(); string f = normname.Capitalize("sarah"); string l = normname.Capitalize("mccloud-o'leary"); string m = normname.Capitalize("henry", true); string fullname = string.Format("{0}, {1} {2}", l, f, m); Console.WriteLine(fullname); //# McCloud-O'Leary, Sarah H. } } }
class NormName { $textinfo = (Get-Culture).TextInfo [string]MC([string]$s) { $a = $s.Split('c', 2) $aFirst = $this.textinfo.ToTitleCase($a[0]).Trim() $aLast = $this.textinfo.ToTitleCase($a[1]).Trim() $n = "{0}c{1}" -f $aFirst, $aLast return $n } [string]SQ([string]$s) { $a = $s.Split("'"); $aFirst = $this.textinfo.ToTitleCase($a[0]).Trim() $aLast = $this.textinfo.ToTitleCase($a[1]).Trim() $n = "{0}'{1}" -f $aFirst, $aLast return $n } [string]HY([string]$s) { $a = $s.Split("-") $aFirst = $a[0].Trim() if ($aFirst.StartsWith("mc")) { $aFirst = $this.MC($aFirst) } elseif ($aFirst.Contains("'")) { $aFirst = $this.SQ($aFirst) } else {$aFirst = $this.textinfo.ToTitleCase($aFirst)} $aLast = $a[1].Trim() if ($aLast.StartsWith("mc")){ $aLast = $this.MC($aLast); } elseif ($aLast.Contains("'")){ $aLast = $this.SQ($aLast); } else {$aLast = $this.textinfo.ToTitleCase($aLast)} $n = "{0}-{1}" -f $aFirst, $aLast return $n } [string]Capitalize([string]$sName, [bool]$initial) { $clean = "" if ($sName) { $prep = $sName.Trim().ToLower() # Married? if ($prep.Contains("-")) { $clean = $this.HY($prep) } elseif ($prep.StartsWith("mc")) { $clean = $this.MC($prep) } elseif ($prep.Contains("'")) { $clean = $this.SQ($prep) } else { $clean = $this.textinfo.ToTitleCase($prep) } # Initial middle name if ($initial -eq $true){ $clean = "{0}." -f $prep[0].ToString().Trim().ToUpper() } } return $clean.Trim() } } # USAGE... CLS $Cleaner = New-Object NormName $First = $Cleaner.Capitalize("sarah",$false) $Last = $Cleaner.Capitalize("herman-rogers",$false) $Mid = $Cleaner.Capitalize("harris",$true) $FullName = "{0}, {1} {2}" -f $Last, $First, $Mid Write-Host $FullName # McCloud-O'Leary, Sarah H.