| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Text.RegularExpressions; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | |
|
| | 7 | | namespace MUNity.Util |
| | 8 | | { |
| | 9 | | public class IdGenerator |
| | 10 | | { |
| 2 | 11 | | private static readonly Random _random = new Random(); |
| | 12 | | /// <summary> |
| | 13 | | /// Gemerates a Random string with all characters form A-Z, a-z and 0-9 |
| | 14 | | /// Total length is 62 |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="length"></param> |
| | 17 | | /// <returns></returns> |
| | 18 | | public static string RandomString(int length) |
| 3 | 19 | | { |
| | 20 | | const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; |
| 3 | 21 | | return new string(Enumerable.Repeat(chars, length) |
| 51 | 22 | | .Select(s => s[_random.Next(s.Length)]).ToArray()); |
| 3 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Turns the text into a lower case verison and removes all special characters |
| | 27 | | /// so the value can be used as primary key (of string) that can also be used for an URL |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="input"></param> |
| | 30 | | /// <returns></returns> |
| | 31 | | public static string AsPrimaryKey(string input) |
| 78 | 32 | | { |
| 78 | 33 | | input = input.ToLower(); |
| 78 | 34 | | input = input.Replace("ä", "ae"); |
| 78 | 35 | | input = input.Replace("ö", "oe"); |
| 78 | 36 | | input = input.Replace("ü", "ue"); |
| 78 | 37 | | input = Regex.Replace(input, "[^a-z0-9]", ""); |
| 78 | 38 | | return input; |
| 78 | 39 | | } |
| | 40 | | } |
| | 41 | | } |