-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringExtensions.cs
66 lines (57 loc) · 2.53 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
using System;
using System.Linq;
namespace DevExGridEnhancer
{
/// <summary>
/// Provides string extension methods for case-insensitive operations and sorting validation.
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Determines whether the string contains a specified substring while ignoring case.
/// </summary>
/// <param name="str">The string to search within.</param>
/// <param name="substring">The substring to search for.</param>
/// <returns>true if the substring is found within the string while ignoring case; otherwise, false.</returns>
public static bool ContainsIgnoringCase(string str, string substring)
{
return str.IndexOf(substring, StringComparison.OrdinalIgnoreCase) >= 0;
}
/// <summary>
/// Determines whether the string contains a specified substring while ignoring case.
/// </summary>
/// <param name="str">The string to search within.</param>
/// <param name="substring">The substring to search for.</param>
/// <returns>true if the substring is found within the string while ignoring case; otherwise, false.</returns>
public static bool IndexOfIgnoringCase(this string str, string substring)
{
return str.IndexOf(substring, StringComparison.OrdinalIgnoreCase) >= 0;
}
/// <summary>
/// Validates a comma-separated list of sort columns.
/// </summary>
/// <param name="sort">The comma-separated list of sort columns to validate.</param>
/// <returns>true if the sort string is valid; otherwise, false.</returns>
public static bool ValidateSort(this string sort)
{
if (string.IsNullOrWhiteSpace(sort)) return true;
var sorted = sort.Trim();
if (sorted.All(char.IsLetterOrDigit))
return true;
var sortColumns = sorted.Split(',').Select(w => w.Trim());
foreach (string column in sortColumns)
{
var cleanText = column;
if (column.StartsWith("[", StringComparison.OrdinalIgnoreCase))
{
// Clean up [ and ]
cleanText = cleanText.Replace('[', ' ');
cleanText = cleanText.Replace(']', ' ');
}
if (!cleanText.All(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)))
return false;
}
return true;
}
}
}