/// <summary> /// Gets the initials from the name /// </summary> /// <param name="maxLength">The maximum number of characters to include in the initials.</param> /// <returns>The initials from the name.</returns> public string ToInitials(int maxLength = 3) { StringBuilder sb = new StringBuilder(); if (First.HasValue()) { sb.Append(First[0]); } if (Middle.HasValue()) { if (Middle.Contains(" ")) { foreach (var v in splitText(Middle)) { sb.Append(v.Trim()[0]); } } else { sb.Append(Middle[0]); } } if (Last.HasValue()) { if (Last.Contains(" ")) { foreach (var v in splitText(Last)) { sb.Append(v.Trim()[0]); } } } else { sb.Append(Last[0]); } if (maxLength < 2) { maxLength = 2; } if (sb.Length > maxLength) { while (sb.Length > maxLength) { sb.Remove(1, 1); } } return(sb.ToString()); }
/// <summary> /// Construct an instance of PersonName by parsing the text provided. /// </summary> /// <param name="text">The text to parse into a name.</param> public PersonName(string text) { if (string.IsNullOrWhiteSpace(text)) { throw new ArgumentNullException(nameof(text)); } string working = text.Trim(); Prefix = string.Empty; Last = string.Empty; First = string.Empty; Middle = string.Empty; Suffix = string.Empty; Nickname = string.Empty; // Check for nicknames (in quotes) if (working.Contains("\"")) { int x1 = working.IndexOf("\""); int x2 = working.IndexOf("\"", x1 + 1); if (x2 > x1) { Nickname = working.Substring(x1 + 1, x2 - x1 - 1).Trim(); working = text.Replace("\"" + Nickname + "\"", string.Empty); } } else if (working.Contains("'")) { int x1 = working.IndexOf("'"); int x2 = working.IndexOf("'", x1 + 1); if (x2 > x1) { Nickname = working.Substring(x1 + 1, x2 - x1 - 1).Trim(); working = working.Replace("'" + Nickname + "'", string.Empty); } } // Check for known prefixes. foreach (var prefix in Honorifics.ForParsingName) { if (working.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { Prefix = prefix.Trim(); working = working.Substring(prefix.Length).Trim(); } } StringBuilder m = new StringBuilder(); // Check to see if text contains comma. if (working.Contains(",")) { int commaInd = working.IndexOf(","); int spaceInd = working.IndexOf(" "); // If a space occurs before the comma, it's most likely formatted as [First Last, Suffix] if (spaceInd > 0 && spaceInd < commaInd) { Suffix = working.Substring(commaInd + 1).Trim(); var split = splitText(working.Substring(0, commaInd)).ToList(); if (split.Count > 2) { First = split.First().Trim(); Last = split.Last().Trim(); foreach (var s in split.Skip(1).Take(split.Count - 2)) { m.Append(" "); m.Append(s); } Middle = m.ToString().Trim(); } else if (split.Count == 2) { First = split.First().Trim(); Last = split.Last().Trim(); } else { Last = split[0].Trim(); } } else // If there is no space before the comma, it's most likely formatted as [Last, First] { Last = working.Substring(0, commaInd).Trim(); working = working.Substring(commaInd + 1).Trim(); if (!working.Contains(" ")) { First = working.Trim(); } else { var split = splitText(working); First = split.First().Trim(); foreach (var s in split.Skip(1)) { m.Append(" "); m.Append(s.Trim()); } Middle = m.ToString().Trim(); // Check to see if there's another comma in the remainer, if so, what follows is a suffix if (Middle.Contains(",")) { Suffix = Middle.Substring(Middle.IndexOf(",") + 1).Trim(); Middle = Middle.Substring(0, Middle.IndexOf(",")).Trim(); } } } } else // No commas. Search for common suffixes and assume it's formatted as [First Last] { if (!working.Contains(" ")) // No space. It's just a last name. { Last = working.Trim(); } else { var split = splitText(working).ToList(); string suff = string.Empty; foreach (var s in split) { if (CommonSuffixes.Contains(s, StringComparer.OrdinalIgnoreCase)) { suff = s; } } if (!string.IsNullOrEmpty(suff)) { split.Remove(suff); Suffix = suff.Trim(); } if (split.Count == 1) { Last = split.First().Trim(); } else if (split.Count == 2) { First = split.First().Trim(); Last = split.Last().Trim(); } else { First = split.First().Trim(); Last = split.Last().Trim(); foreach (var s in split.Skip(1).Take(split.Count - 2)) { m.Append(" "); m.Append(s); } Middle = m.ToString().Trim(); } } } }