Repository navigation
Expand file tree
/
Copy pathText_Before.cs
More file actions
45 lines (41 loc) · 2.17 KB
/
Copy pathText_Before.cs
File metadata and controls
45 lines (41 loc) · 2.17 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
using System;
using static System.StringComparison;
namespace ToSic.Razor.Blade
{
public static partial class Text
{
/// <summary>
/// Get string value before the key.
/// Case insensitive by default.
/// Will return null in various cases which don't make sense or if not found.
/// Very safe and robust, fully tested.
/// </summary>
/// <param name="value">The initial string. If null, will always return null</param>
/// <param name="key">The key to find. If null, will always return null</param>
/// <param name="caseSensitive">Set to true if you need case-sensitive compare</param>
/// <returns></returns>
/// <remarks>New in v3.09</remarks>
public static string Before(this string value, string key, bool caseSensitive = false)
=> value.BeforeInternal(key, caseSensitive ? Ordinal : InvariantCultureIgnoreCase);
/// <summary>
/// Get string value before the last occurrence of a key.
/// Case insensitive by default.
/// Will return null in various cases which don't make sense or if not found.
/// Very safe and robust, fully tested.
/// </summary>
/// <param name="value">The initial string. If null, will always return null</param>
/// <param name="key">The key to find. If null, will always return null</param>
/// <param name="caseSensitive">Set to true if you need case-sensitive compare</param>
/// <returns></returns>
/// <remarks>New in v3.09</remarks>
public static string BeforeLast(this string value, string key, bool caseSensitive = false)
=> value.BeforeInternal(key, caseSensitive ? Ordinal : InvariantCultureIgnoreCase, true);
private static string BeforeInternal(this string value, string key, StringComparison comparison, bool findLast = false, string fallback = null)
{
if (value == null || key == null) return fallback;
var posA = findLast ? value.LastIndexOf(key, comparison) : value.IndexOf(key, comparison);
if (posA == -1) return fallback;
return value.Substring(0, posA);
}
}
}