-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathStringExtensions.cs
69 lines (51 loc) · 2.27 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
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// ReSharper disable once CheckNamespace
namespace System;
public static class StringExtensions
{
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? value)
=> string.IsNullOrWhiteSpace(value);
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? value)
=> string.IsNullOrEmpty(value);
public static string TrimStart(this string value, string trimParameter)
=> value.TrimStart(trimParameter, StringComparison.CurrentCulture);
public static string TrimStart(this string value,
string trimParameter,
StringComparison stringComparison)
{
if (!value.StartsWith(trimParameter, stringComparison))
return value;
return value.Substring(trimParameter.Length);
}
public static string TrimEnd(this string value, string trimParameter)
=> value.TrimEnd(trimParameter, StringComparison.CurrentCulture);
public static string TrimEnd(this string value,
string trimParameter,
StringComparison stringComparison)
{
if (!value.EndsWith(trimParameter, stringComparison))
return value;
return value.Substring(0, value.Length - trimParameter.Length);
}
public static byte[] ConvertToBytes(this string value, Encoding encoding)
=> encoding.GetBytes(value);
public static byte[] FromBase64String(this string value)
=> Convert.FromBase64String(value);
public static string GetSpecifiedLengthString(
this string value,
int length,
Action action,
FillType fillType = FillType.NoFile,
char fillCharacter = ' ')
{
if (fillType == FillType.NoFile && value.Length < length)
action.Invoke();
var keyLength = value.Length;
if (keyLength == length) return value;
if (keyLength > length) return value.Substring(0, length);
if (fillType == FillType.Left) return value.PadLeft(length, fillCharacter);
if (fillType == FillType.Right) return value.PadRight(length, fillCharacter);
throw new NotSupportedException($"... Unsupported {nameof(fillType)}");
}
}