-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringExtensions.cs
52 lines (43 loc) · 1.86 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
using System;
namespace Samples.Common.Extensions
{
public static class StringExtensions
{
public static bool ContainsIgnoringCase(this string source, string substring)
{
return Contains(source, substring, StringComparison.InvariantCultureIgnoreCase);
}
public static bool Contains(this string source, string substring, StringComparison stringComparison)
{
if (string.IsNullOrEmpty(source))
{
return false;
}
if (substring == null)
{
throw new ArgumentNullException(nameof(substring), $"{nameof(substring)} cannot be null.");
}
if (!Enum.IsDefined(typeof(StringComparison), stringComparison))
{
throw new ArgumentException($"{nameof(stringComparison)} is not a member of StringComparison", nameof(stringComparison));
}
return source.IndexOf(substring, stringComparison) >= 0;
}
public static bool StartsWithIgnoringCase(this string source, string substring)
{
return StartsWith(source, substring, StringComparison.InvariantCultureIgnoreCase);
}
public static bool StartsWith(this string source, string substring, StringComparison stringComparison)
{
if (substring == null)
{
throw new ArgumentNullException(nameof(substring), $"{nameof(substring)} cannot be null.");
}
if (!Enum.IsDefined(typeof(StringComparison), stringComparison))
{
throw new ArgumentException($"{nameof(stringComparison)} is not a member of StringComparison", nameof(stringComparison));
}
return source.IndexOf(substring, stringComparison) == 0;
}
}
}