using System; using System.Linq; using System.Text; using System.Text.RegularExpressions; using UnityEngine; namespace DTT.Utils.Extensions { /// /// A static class providing extension methods for handling strings. /// public static class StringExtensions { /// /// The regex used for stripping html tags from strings. /// public static readonly Regex htmlRegex = new Regex(@"<.*?>", RegexOptions.Compiled); /// /// Prefixes used for private members. /// public static readonly string[] privateMemberPrefixes = new string[] { "_", "m_" }; /// /// Returns the display name for a field removing for example an underscore (_) from the name. /// /// The field name to convert. /// The display name. public static string ToDisplayName(this string fieldName) { StringBuilder stringBuilder = new StringBuilder(fieldName); for (int i = 0; i < privateMemberPrefixes.Length; i++) { if (fieldName.StartsWith(privateMemberPrefixes[i])) { stringBuilder.Remove(0, privateMemberPrefixes[i].Length); break; } } stringBuilder[0] = char.ToUpper(stringBuilder[0]); return stringBuilder.ToString(); } /// /// Returns the string with spaces before capitals. /// /// The string to base the new string on. /// The string with spaces before capitals. public static string AddSpacesBeforeCapitals(this string content) { if (content == null) return null; StringBuilder sb = new StringBuilder(content); for (int i = sb.Length - 2; i >= 1; i--) if (char.IsUpper(sb[i]) && char.IsLower(sb[i + 1]) && sb[i - 1] != ' ') sb.Insert(i, " "); return sb.ToString(); } /// /// Converts a string of constant styling (MY_CONSTANT) to a /// readable format (My Constant). /// /// The content to convert. /// The converted string. public static string FromAllCapsToReadableFormat(this string content) { if (string.IsNullOrEmpty(content)) return content; content = content.ToLower(); string[] separate = content.Split('_'); StringBuilder sb = new StringBuilder(); for (int i = 0; i < separate.Length; i++) { separate[i] = separate[i][0].ToString().ToUpper() + separate[i].Substring(1); sb.Append(separate[i]); if (i != separate.Length - 1) sb.Append(' '); } return sb.ToString(); } /// /// Converts a string of readable format (My Constant) to a /// constant styling (MY_CONSTANT). /// /// The content to convert. /// The converted string. public static string FromReadableFormatToAllCaps(this string content) { if (string.IsNullOrEmpty(content)) return content; return content.ToUpper().Replace(' ', '_'); ; } /// /// Returns the index of the 'nth' appearance of a string. /// /// The value in which the string appears. /// The string. /// The number of finds after which to stop. /// The index of the 'nth' appearance. public static int IndexOfNth(this string @string, string value, int nth) { if (nth < 0) throw new ArgumentOutOfRangeException(nameof(nth)); if (@string == null) throw new ArgumentNullException(nameof(@string)); if (value == null) throw new ArgumentNullException(nameof(value)); int offset = @string.IndexOf(value, StringComparison.Ordinal); for (int i = 0; i < nth - 1; i++) { if (offset == -1) return -1; offset = @string.IndexOf(value, offset + 1, StringComparison.Ordinal); } return offset; } /// /// Returns the index of the 'nth' appearance of a character. /// /// The value in which the character appears. /// The character. /// The number of finds after which to stop. /// The index of the 'nth' appearance. public static int IndexOfNth(this string @string, char value, int nth) => IndexOfNth(@string, char.ToString(value), nth); /// /// Returns whether the string corresponds with a valid guid. /// /// The string to check. /// Whether the string corresponds with a valid guid. public static bool IsValidGuid(this string @string) { if (@string == null) throw new ArgumentNullException(nameof(@string)); return Guid.TryParse(@string, out _); } /// /// Returns whether the string corresponds with a valid guid with exact format. /// /// The string to check. /// The format to check. /// Whether the string corresponds with a valid guid. public static bool IsValidGuid(this string @string, string format) { if (@string == null) throw new ArgumentNullException(nameof(@string)); if (format == null) throw new ArgumentNullException(nameof(format)); return Guid.TryParseExact(@string, format, out _); } /// /// Strips html tags from a string. /// /// The text string to remove html tags from. /// The text stripped from tags. public static string StripHtmlTags(this string text) { if (text == null) throw new ArgumentNullException(nameof(text)); return htmlRegex.Replace(text, string.Empty); } /// /// Returns string with ellipsis characters at the end if the input string /// its width is greater than the given max width value. /// /// The input string. /// The maximum width of the string. /// The font that is used. public static string Ellipsis(this string input, int maxWidth, Font font) => Ellipsis(input, maxWidth, '.', font); /// /// Returns string with ellipsis characters at the end if the input string /// its width is greater than the given max width value. /// /// The input string. /// The maximum width of the string. /// The ellipsis character that is used. /// The font that is used. public static string Ellipsis(this string input, int maxWidth, char ellipsisChar, Font font, int characterCount = 3) { if (input == null) throw new ArgumentNullException(nameof(input)); if (font == null) throw new ArgumentNullException(nameof(font)); char[] chars = input.ToCharArray(); int totalLength = 0; CharacterInfo info; string text = input; for (int j = 0; j < chars.Length; j++) { font.GetCharacterInfo(chars[j], out info); totalLength += info.advance; if (totalLength > maxWidth) { text = text.Substring(0, Mathf.Max(j - characterCount, 0)); text += new string(Enumerable.Repeat(ellipsisChar, characterCount).ToArray()); break; } } return text; } } }