IndieWafflus

TextUtility

Jul 14th, 2021 (edited)
3,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. public static class TextUtility
  2. {
  3.     public static bool IsWhitespace(this char character)
  4.     {
  5.         switch (character)
  6.         {
  7.             case '\u0020':
  8.             case '\u00A0':
  9.             case '\u1680':
  10.             case '\u2000':
  11.             case '\u2001':
  12.             case '\u2002':
  13.             case '\u2003':
  14.             case '\u2004':
  15.             case '\u2005':
  16.             case '\u2006':
  17.             case '\u2007':
  18.             case '\u2008':
  19.             case '\u2009':
  20.             case '\u200A':
  21.             case '\u202F':
  22.             case '\u205F':
  23.             case '\u3000':
  24.             case '\u2028':
  25.             case '\u2029':
  26.             case '\u0009':
  27.             case '\u000A':
  28.             case '\u000B':
  29.             case '\u000C':
  30.             case '\u000D':
  31.             case '\u0085':
  32.             {
  33.                 return true;
  34.             }
  35.  
  36.             default:
  37.             {
  38.                 return false;
  39.             }
  40.         }
  41.     }
  42.  
  43.     // While unnecessary for this project, I've used the method seen here: https://stackoverflow.com/a/37368176
  44.     // Benchmarks: https://stackoverflow.com/a/37347881
  45.     public static string RemoveWhitespaces(this string text)
  46.     {
  47.         int textLength = text.Length;
  48.  
  49.         char[] textCharacters = text.ToCharArray();
  50.  
  51.         int currentWhitespacelessTextLength = 0;
  52.  
  53.         for (int currentCharacterIndex = 0; currentCharacterIndex < textLength; ++currentCharacterIndex)
  54.         {
  55.             char currentTextCharacter = textCharacters[currentCharacterIndex];
  56.  
  57.             if (currentTextCharacter.IsWhitespace())
  58.             {
  59.                 continue;
  60.             }
  61.  
  62.             textCharacters[currentWhitespacelessTextLength++] = currentTextCharacter;
  63.         }
  64.  
  65.         return new string(textCharacters, 0, currentWhitespacelessTextLength);
  66.     }
  67.  
  68.     // See here for alternatives: https://stackoverflow.com/questions/3210393/how-do-i-remove-all-non-alphanumeric-characters-from-a-string-except-dash
  69.     public static string RemoveSpecialCharacters(this string text)
  70.     {
  71.         int textLength = text.Length;
  72.  
  73.         char[] textCharacters = text.ToCharArray();
  74.  
  75.         int currentWhitespacelessTextLength = 0;
  76.  
  77.         for (int currentCharacterIndex = 0; currentCharacterIndex < textLength; ++currentCharacterIndex)
  78.         {
  79.             char currentTextCharacter = textCharacters[currentCharacterIndex];
  80.  
  81.             if (!char.IsLetterOrDigit(currentTextCharacter) && !currentTextCharacter.IsWhitespace())
  82.             {
  83.                 continue;
  84.             }
  85.  
  86.             textCharacters[currentWhitespacelessTextLength++] = currentTextCharacter;
  87.         }
  88.  
  89.         return new string(textCharacters, 0, currentWhitespacelessTextLength);
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment