-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiterals.cs
More file actions
45 lines (38 loc) · 1.86 KB
/
Literals.cs
File metadata and controls
45 lines (38 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.Diagnostics.CodeAnalysis;
namespace TheElm.Literals {
public static partial class Literals {
#region Constants
public const string NUMBERIC = "0123456789";
public const string ALPHANUMERICAL = $"{Literals.ALPHABETIC}{Literals.NUMBERIC}";
public const string ALPHABETIC = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const string UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const string LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
public const string NUMERICS = "0123456789";
public const string ALL_VALID = $"{Literals.ALPHABETIC}{Literals.NUMERICS}";
#endregion
/// <summary>
/// Create a Substring of the given string, even if the given length is out of bounds of the string
/// </summary>
/// <param name="input"></param>
/// <param name="max"></param>
/// <returns></returns>
[return: NotNullIfNotNull(nameof(input))]
public static string? SubstringMax( this string? input, int max )
=> input is not null && input.Length > max ? input[..max] : input;
/// <summary>
/// Get the number of matching characters from the start of two strings
/// "Apple" and "Banana" would be 0, but "Happy" and "Hapless" would be 3
/// </summary>
/// <param name="a">First input string</param>
/// <param name="b">Second input string</param>
/// <returns>Number of matching characters from the beginning</returns>
public static int MatchingStartCharacters( string a, string b ) {
int len = Math.Min(a.Length, b.Length);
for (int i = 0; i < len; i++) {
if (!a[i].Equals(b[i]))
return i - 1;
}
return len;
}
}
}