-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
65 lines (46 loc) · 1.47 KB
/
StringExtensions.cs
File metadata and controls
65 lines (46 loc) · 1.47 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Text;
using System.Text.RegularExpressions;
namespace Netcorext.Extensions.Commons;
public static class StringExtensions
{
#region Encoding
public static byte[] ToUtf8Bytes(this string source)
{
return Encoding.UTF8.GetBytes(source);
}
#endregion
#region Convert
public static string ToLowerCamelCase(this string source)
{
if (string.IsNullOrWhiteSpace(source)) return string.Empty;
if (source.Length == 1) return source.ToLower();
if (char.IsUpper(source[0])) return char.ToLower(source[0]) + source.Substring(1);
return source;
}
public static string ToSnakeCase(this string source)
{
return Regex.Replace(source, "(?<!_|\\b)([A-Z])", "_$1", RegexOptions.Compiled)
.ToLower();
}
#endregion
#region Capture
public static string Left(this string source, int length)
{
if (string.IsNullOrWhiteSpace(source))
return null;
return source.Length <= length
? source
: source.Substring(0, length);
}
public static string Right(this string source, int length)
{
if (string.IsNullOrWhiteSpace(source))
return null;
if (string.IsNullOrWhiteSpace(source))
return null;
return source.Length - length <= 0
? source
: source.Substring(source.Length - length, length);
}
#endregion
}