-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStringExtensions.cs
72 lines (69 loc) · 3.92 KB
/
StringExtensions.cs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Semver;
namespace VolumeControl.TypeExtensions
{
/// <summary>
/// Extensions for string types.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Parses a string containing a version number in semantic versioning 2 format.
/// </summary>
public static SemVersion? GetSemVer(this string? s)
{
if (string.IsNullOrWhiteSpace(s)) return null;
if (SemVersion.TryParse(s.Trim(), SemVersionStyles.OptionalPatch, out SemVersion result))
{
return result;
}
return null;
}
/// <summary>
/// Removes all chars that <paramref name="pred"/> returns true for.
/// </summary>
public static string RemoveIf(this string s, Predicate<char> pred)
{
for (int i = s.Length - 1; i >= 0; --i)
{
if (pred(s[i]))
s = s.Remove(i, i);
}
return s;
}
/// <summary>
/// Removes all preceeding/trailing occurrences of the specified characters from a string.
/// </summary>
/// <param name="s">The input string.</param>
/// <param name="trimChars">Any number of characters in a string.</param>
/// <returns>String with all preceeding/trailing characters from trimChars removed.</returns>
public static string Trim(this string s, string trimChars)
=> s.Trim(trimChars.ToCharArray());
/// <summary>
/// Gets the <see langword="char"/> at <paramref name="index"/>, or <paramref name="defaultChar"/> if the index is out of range.
/// </summary>
/// <param name="s">The string that this extension method was called on.</param>
/// <param name="index">The target index within the string to access.</param>
/// <param name="defaultChar">A character to return when the index is out-of-range.</param>
/// <returns>The character at <paramref name="index"/> in <paramref name="s"/> if the index is within range; otherwise <paramref name="defaultChar"/>.</returns>
public static char AtIndexOrDefault(this string s, int index, char defaultChar) => index < s.Length ? s[index] : defaultChar;
/// <summary>
/// Gets the <see langword="char"/> at <paramref name="index"/>, or the result of the <see langword="default"/> keyword if the index is out of range.
/// </summary>
/// <returns>The character at <paramref name="index"/> in <paramref name="s"/> if the index is within range; otherwise <see langword="default"/>.</returns>
/// <inheritdoc cref="AtIndexOrDefault(string, int, char)"/>
public static char AtIndexOrDefault(this string s, int index) => AtIndexOrDefault(s, index, default);
/// <summary>
/// Check if <paramref name="s"/> equals any of the given <paramref name="compare"/> strings using <paramref name="sCompareType"/> comparison.
/// </summary>
/// <param name="s">A string.</param>
/// <param name="sCompareType">The <see cref="StringComparison"/> to use.</param>
/// <param name="compare">Any number of strings to compare to <paramref name="s"/>.</param>
/// <returns><see langword="true"/> when <paramref name="s"/> equals at least one of the <paramref name="compare"/> strings; otherwise <see langword="false"/></returns>
public static bool EqualsAny(this string s, StringComparison sCompareType, params string[] compare) => compare.Any(c => s.Equals(c, sCompareType));
/// <summary>
/// Check if <paramref name="s"/> equals any of the given <paramref name="compare"/> strings using <see cref="StringComparison.Ordinal"/> comparison.
/// </summary>
/// <inheritdoc cref="EqualsAny(string, StringComparison, string[])"/>
public static bool EqualsAny(this string s, params string[] compare) => EqualsAny(s, StringComparison.Ordinal, compare);
}
}