/// <summary> /// To capital case /// </summary> /// <param name="text"></param> /// <returns></returns> public static string ToCapitalCase(string text) { var result = new List <string>(); foreach (var word in text.SplitByWords()) { if (word.Length == 0 || StringWordHelper.AllCapitals(word)) { result.Add(word); } else if (word.Length == 1) { result.Add(word.ToUpper()); } else { result.Add(char.ToUpper(word[0]) + word.Remove(0, 1).ToLower()); } } return(string.Join(" ", result) .Replace(" Y ", " y ") .Replace(" De ", " de ") .Replace(" O ", " o ")); }
/// <summary> /// Truncate By Words /// </summary> /// <param name="text"></param> /// <param name="maxNumber"></param> /// <param name="placeholder"></param> /// <param name="extraSpace">额外的空格</param> /// <returns></returns> public static string TruncateByWords(string text, int maxNumber, string placeholder = "...", bool extraSpace = false) { if (string.IsNullOrEmpty(text) || maxNumber <= 0) { return(string.Empty); } if (string.IsNullOrEmpty(placeholder)) { placeholder = "..."; } var result = new List <string>(); var counter = 0; var touchBoundary = false; foreach (var word in text.SplitByWords()) { if (counter++ == maxNumber) { touchBoundary = true; break; } result.Add(word); } return(StringWordHelper.AppendEndIfNeed( string.Join(" ", result), extraSpace ? $" {placeholder}" : placeholder, () => touchBoundary)); }