|
1 | 1 | using System;
|
| 2 | +using System.Globalization; |
2 | 3 | using System.Text;
|
3 | 4 |
|
4 |
| -namespace KY.Core |
| 5 | +namespace KY.Core; |
| 6 | + |
| 7 | +public static class StringExtension |
5 | 8 | {
|
6 |
| - public static class StringExtension |
| 9 | + public static T ToEnum<T>(this string value) |
7 | 10 | {
|
8 |
| - public static T ToEnum<T>(this string value) |
9 |
| - { |
10 |
| - return (T)Enum.Parse(typeof(T), value); |
11 |
| - } |
| 11 | + return (T)Enum.Parse(typeof(T), value); |
| 12 | + } |
12 | 13 |
|
13 |
| - public static string SubstringSafe(this string value, int startIndex) |
| 14 | + public static string SubstringSafe(this string value, int startIndex) |
| 15 | + { |
| 16 | + if (value == null) |
14 | 17 | {
|
15 |
| - if (value == null) |
16 |
| - throw new NullReferenceException(); |
17 |
| - |
18 |
| - if (startIndex > value.Length) |
19 |
| - return string.Empty; |
20 |
| - return value.Substring(startIndex); |
| 18 | + throw new NullReferenceException(); |
21 | 19 | }
|
22 | 20 |
|
23 |
| - public static string SubstringSafe(this string value, int startIndex, int length) |
| 21 | + if (startIndex > value.Length) |
24 | 22 | {
|
25 |
| - if (value == null) |
26 |
| - throw new NullReferenceException(); |
27 |
| - |
28 |
| - if (startIndex > value.Length) |
29 |
| - return string.Empty; |
30 |
| - |
31 |
| - length = Math.Min(value.Length - startIndex, length); |
32 |
| - return value.Substring(startIndex, length); |
| 23 | + return string.Empty; |
33 | 24 | }
|
| 25 | + return value.Substring(startIndex); |
| 26 | + } |
34 | 27 |
|
35 |
| - public static string Format(this string format, params object[] args) |
| 28 | + public static string SubstringSafe(this string value, int startIndex, int length) |
| 29 | + { |
| 30 | + if (value == null) |
36 | 31 | {
|
37 |
| - return string.Format(format, args); |
| 32 | + throw new NullReferenceException(); |
38 | 33 | }
|
39 | 34 |
|
40 |
| - public static string Trim(this string value, string trim) |
| 35 | + if (startIndex > value.Length) |
41 | 36 | {
|
42 |
| - return value.TrimStart(trim).TrimEnd(trim); |
| 37 | + return string.Empty; |
43 | 38 | }
|
44 | 39 |
|
45 |
| - public static string TrimEnd(this string value, string trim) |
| 40 | + length = Math.Min(value.Length - startIndex, length); |
| 41 | + return value.Substring(startIndex, length); |
| 42 | + } |
| 43 | + |
| 44 | + public static string Format(this string format, params object[] args) |
| 45 | + { |
| 46 | + return string.Format(format, args); |
| 47 | + } |
| 48 | + |
| 49 | + public static string Trim(this string value, string trim) |
| 50 | + { |
| 51 | + return value.TrimStart(trim).TrimEnd(trim); |
| 52 | + } |
| 53 | + |
| 54 | + public static string TrimEnd(this string value, string trim) |
| 55 | + { |
| 56 | + while (value.EndsWith(trim)) |
46 | 57 | {
|
47 |
| - while (value.EndsWith(trim)) |
48 |
| - { |
49 |
| - value = value.Substring(0, value.Length - trim.Length); |
50 |
| - } |
51 |
| - return value; |
| 58 | + value = value.Substring(0, value.Length - trim.Length); |
52 | 59 | }
|
| 60 | + return value; |
| 61 | + } |
53 | 62 |
|
54 |
| - public static string TrimStart(this string value, string trim) |
| 63 | + public static string TrimStart(this string value, string trim) |
| 64 | + { |
| 65 | + while (value.StartsWith(trim)) |
55 | 66 | {
|
56 |
| - while (value.StartsWith(trim)) |
57 |
| - { |
58 |
| - value = value.Substring(trim.Length); |
59 |
| - } |
60 |
| - return value; |
| 67 | + value = value.Substring(trim.Length); |
61 | 68 | }
|
| 69 | + return value; |
| 70 | + } |
62 | 71 |
|
63 |
| - public static string Remove(this string value, string toRemove) |
| 72 | + public static string Remove(this string value, string toRemove) |
| 73 | + { |
| 74 | + return value?.Replace(toRemove, string.Empty); |
| 75 | + } |
| 76 | + |
| 77 | + public static string Repeat(this string value, int length) |
| 78 | + { |
| 79 | + while (value.Length < length) |
64 | 80 | {
|
65 |
| - return value?.Replace(toRemove, string.Empty); |
| 81 | + value += value; |
66 | 82 | }
|
| 83 | + return value.Substring(0, length); |
| 84 | + } |
67 | 85 |
|
68 |
| - public static string Repeate(this string value, int length) |
| 86 | + public static string FirstCharToLower(this string value) |
| 87 | + { |
| 88 | + if (!string.IsNullOrEmpty(value) && value != value.ToUpper()) |
69 | 89 | {
|
70 |
| - while (value.Length < length) |
71 |
| - { |
72 |
| - value += value; |
73 |
| - } |
74 |
| - return value.Substring(0, length); |
| 90 | + value = value[0].ToString().ToLower() + value.Substring(1); |
75 | 91 | }
|
| 92 | + return value; |
| 93 | + } |
| 94 | + |
| 95 | + public static string FirstCharToUpper(this string value) |
| 96 | + { |
| 97 | + return string.IsNullOrEmpty(value) ? value : value[0].ToString().ToUpper() + value.Substring(1); |
| 98 | + } |
76 | 99 |
|
77 |
| - public static string FirstCharToLower(this string value) |
| 100 | + public static string Fallback(this string value, string fallback) |
| 101 | + { |
| 102 | + return string.IsNullOrEmpty(value) ? fallback : value; |
| 103 | + } |
| 104 | + |
| 105 | + public static string PadLeft(this string value, int totalWidth, string text = " ", bool exact = false) |
| 106 | + { |
| 107 | + if (value == null || text == null || value.Length >= totalWidth) |
78 | 108 | {
|
79 |
| - if (!string.IsNullOrEmpty(value) && value != value.ToUpper()) |
80 |
| - { |
81 |
| - value = value[0].ToString().ToLower() + value.Substring(1); |
82 |
| - } |
83 | 109 | return value;
|
84 | 110 | }
|
85 |
| - |
86 |
| - public static string FirstCharToUpper(this string value) |
| 111 | + StringBuilder builder = new(); |
| 112 | + while (builder.Length + value.Length < totalWidth) |
87 | 113 | {
|
88 |
| - return string.IsNullOrEmpty(value) ? value : value[0].ToString().ToUpper() + value.Substring(1); |
| 114 | + builder.Append(text); |
89 | 115 | }
|
| 116 | + builder.Append(value); |
| 117 | + return exact ? builder.ToString().Substring(0, totalWidth) : builder.ToString(); |
| 118 | + } |
90 | 119 |
|
91 |
| - public static string Fallback(this string value, string fallback) |
| 120 | + public static string PadRight(this string value, int totalWidth, string text = " ", bool exact = false) |
| 121 | + { |
| 122 | + if (value == null || text == null || value.Length >= totalWidth) |
92 | 123 | {
|
93 |
| - return string.IsNullOrEmpty(value) ? fallback : value; |
| 124 | + return value; |
94 | 125 | }
|
95 |
| - |
96 |
| - public static string PadLeft(this string value, int totalWidth, string text = " ", bool exact = false) |
| 126 | + StringBuilder builder = new(); |
| 127 | + builder.Append(value); |
| 128 | + while (builder.Length < totalWidth) |
97 | 129 | {
|
98 |
| - if (value == null || text == null || value.Length >= totalWidth) |
99 |
| - { |
100 |
| - return value; |
101 |
| - } |
102 |
| - StringBuilder builder = new(); |
103 |
| - while (builder.Length + value.Length < totalWidth) |
104 |
| - { |
105 |
| - builder.Append(text); |
106 |
| - } |
107 |
| - builder.Append(value); |
108 |
| - return exact ? builder.ToString().Substring(0, totalWidth) : builder.ToString(); |
| 130 | + builder.Append(text); |
109 | 131 | }
|
| 132 | + return exact ? builder.ToString().Substring(0, totalWidth) : builder.ToString(); |
| 133 | + } |
110 | 134 |
|
111 |
| - public static string PadRight(this string value, int totalWidth, string text = " ", bool exact = false) |
112 |
| - { |
113 |
| - if (value == null || text == null || value.Length >= totalWidth) |
114 |
| - { |
115 |
| - return value; |
116 |
| - } |
117 |
| - StringBuilder builder = new(); |
118 |
| - builder.Append(value); |
119 |
| - while (builder.Length < totalWidth) |
120 |
| - { |
121 |
| - builder.Append(text); |
122 |
| - } |
123 |
| - return exact ? builder.ToString().Substring(0, totalWidth) : builder.ToString(); |
124 |
| - } |
| 135 | + public static bool ContainsInvariant(this string source, string value, CompareOptions compareOptions = CompareOptions.IgnoreCase) |
| 136 | + { |
| 137 | + return source != null && value != null && CultureInfo.InvariantCulture.CompareInfo.IndexOf(source, value, compareOptions) >= 0; |
125 | 138 | }
|
126 | 139 | }
|
0 commit comments