-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
66 lines (59 loc) · 2.92 KB
/
StringExtensions.cs
File metadata and controls
66 lines (59 loc) · 2.92 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
66
using Newtonsoft.Json.Serialization;
namespace CSharpExtensions.OpenSource
{
public static class StringExtensions
{
public static string CleanupNewline(this string text) => text.Replace("\n", "")
.Replace("\r", "")
.Replace("\r\n", "")
.Replace("\\\n", "")
.Replace("\\\r", "")
.Replace("\\\r\\\n", "")
.Replace("\\n", "")
.Replace("\\r", "")
.Replace("\\r\\n", "");
public static string? ToCamelCase(this string? str) => str is null
? null
: new DefaultContractResolver() { NamingStrategy = new CamelCaseNamingStrategy() }.GetResolvedPropertyName(str);
public static string? ToSnakeCase(this string? str) => str is null
? null
: new DefaultContractResolver() { NamingStrategy = new SnakeCaseNamingStrategy() }.GetResolvedPropertyName(str);
public static string? DetectAsNull(this string? str, params string[] vals)
{
var res = str?.CleanupNewline().Trim();
if (string.IsNullOrEmpty(res)) { return null; }
foreach (var val in vals)
{
if (res == val) { return null; }
}
return str;
}
/// <summary>
/// Method that limits the length of text to a defined length.
/// </summary>
/// <param name="source">The source text.</param>
/// <param name="maxLength">The maximum limit of the string to return.</param>
public static string LimitLength(this string source, int maxLength)
{
if (source.Length <= maxLength) { return source; }
return source.Substring(0, maxLength);
}
public static string TrimStart(this string target, string? trimString)
{
if (string.IsNullOrEmpty(trimString)) { return target; }
var result = target;
while (result.StartsWith(trimString)) { result = result.Substring(trimString.Length); }
return result;
}
public static string TrimEnd(this string target, string? trimString)
{
if (string.IsNullOrEmpty(trimString)) { return target; }
var result = target;
while (result.EndsWith(trimString)) { result = result.Substring(0, result.Length - trimString.Length); }
return result;
}
public static string Trim(this string target, string? trimString) => target.TrimStart(trimString).TrimEnd(trimString);
// check if string empty
public static bool IsEmpty(this string? str) => str == null || string.IsNullOrEmpty(str);
}
}