Skip to content

Commit 6b7e0b6

Browse files
committed
String and enumerable helper updated
1 parent 7938466 commit 6b7e0b6

File tree

3 files changed

+103
-85
lines changed

3 files changed

+103
-85
lines changed

Core.Common.Standard/Extension/EnumerableExtension.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,10 @@ public static T SecondOrDefault<T>(this IEnumerable<T> source, Func<T, bool> pre
199199
{
200200
return source.Skip(1).FirstOrDefault(predicate);
201201
}
202+
203+
public static IEnumerable<T> RemoveNulls<T>(this IEnumerable<T?> source)
204+
{
205+
return source.Where(x => x != null);
206+
}
202207
}
203-
}
208+
}
Lines changed: 96 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,139 @@
11
using System;
2+
using System.Globalization;
23
using System.Text;
34

4-
namespace KY.Core
5+
namespace KY.Core;
6+
7+
public static class StringExtension
58
{
6-
public static class StringExtension
9+
public static T ToEnum<T>(this string value)
710
{
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+
}
1213

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)
1417
{
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();
2119
}
2220

23-
public static string SubstringSafe(this string value, int startIndex, int length)
21+
if (startIndex > value.Length)
2422
{
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;
3324
}
25+
return value.Substring(startIndex);
26+
}
3427

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)
3631
{
37-
return string.Format(format, args);
32+
throw new NullReferenceException();
3833
}
3934

40-
public static string Trim(this string value, string trim)
35+
if (startIndex > value.Length)
4136
{
42-
return value.TrimStart(trim).TrimEnd(trim);
37+
return string.Empty;
4338
}
4439

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))
4657
{
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);
5259
}
60+
return value;
61+
}
5362

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))
5566
{
56-
while (value.StartsWith(trim))
57-
{
58-
value = value.Substring(trim.Length);
59-
}
60-
return value;
67+
value = value.Substring(trim.Length);
6168
}
69+
return value;
70+
}
6271

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)
6480
{
65-
return value?.Replace(toRemove, string.Empty);
81+
value += value;
6682
}
83+
return value.Substring(0, length);
84+
}
6785

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())
6989
{
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);
7591
}
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+
}
7699

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)
78108
{
79-
if (!string.IsNullOrEmpty(value) && value != value.ToUpper())
80-
{
81-
value = value[0].ToString().ToLower() + value.Substring(1);
82-
}
83109
return value;
84110
}
85-
86-
public static string FirstCharToUpper(this string value)
111+
StringBuilder builder = new();
112+
while (builder.Length + value.Length < totalWidth)
87113
{
88-
return string.IsNullOrEmpty(value) ? value : value[0].ToString().ToUpper() + value.Substring(1);
114+
builder.Append(text);
89115
}
116+
builder.Append(value);
117+
return exact ? builder.ToString().Substring(0, totalWidth) : builder.ToString();
118+
}
90119

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)
92123
{
93-
return string.IsNullOrEmpty(value) ? fallback : value;
124+
return value;
94125
}
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)
97129
{
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);
109131
}
132+
return exact ? builder.ToString().Substring(0, totalWidth) : builder.ToString();
133+
}
110134

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;
125138
}
126139
}

Core.Common.Standard/KY.Core.Common.Standard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>KY.Core</RootNamespace>
66
<AssemblyName>KY.Core.Common</AssemblyName>
7-
<Version>4.25.0</Version>
7+
<Version>4.26.0</Version>
88
<Authors>KY-Programming</Authors>
99
<Company>KY-Programmingp</Company>
1010
<Product>KY.Core</Product>

0 commit comments

Comments
 (0)