Guest User

Untitled

a guest
Feb 20th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. private static string CreateId(string inputstr)
  2. {
  3.         char[] allowed_chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '_' };
  4.         inputstr = inputstr.Replace("á", "a")
  5.                            .Replace("í", "i")
  6.                            .Replace("é", "e")
  7.                            .Replace("ó", "o")
  8.                            .Replace("ö", "o")
  9.                            .Replace("ő", "o")
  10.                            .Replace("ü", "u")
  11.                            .Replace("ú", "u")
  12.                            .Replace("ű", "u")
  13.                            .Replace("ä", "a")
  14.                            .Replace("ş", "s")
  15.                            .Replace("ô", "o")
  16.                            .Replace(" ", "_")
  17.                            .Replace("-", "_");
  18.         StringBuilder sb = new StringBuilder();
  19.         bool wasHyphen = true;
  20.  
  21.         foreach (char c in inputstr)
  22.         {
  23.                 if (char.IsLetterOrDigit(c))
  24.                 {
  25.                         if (allowed_chars.Contains(c))
  26.                         {
  27.                                 sb.Append(c);
  28.                                 wasHyphen = false;
  29.                         }
  30.                 }
  31.                 else if (c != '\'' && !wasHyphen)
  32.                 {
  33.                         sb.Append('-');
  34.                         wasHyphen = true;
  35.                 }
  36.         }
  37.  
  38.         // Avoid trailing hyphens
  39.         if (wasHyphen && sb.Length > 0)
  40.                 sb.Length--;
  41.  
  42.         return sb.ToString();
  43. }
Advertisement
Add Comment
Please, Sign In to add comment