-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathText_Between.cs
More file actions
43 lines (39 loc) · 1.9 KB
/
Copy pathText_Between.cs
File metadata and controls
43 lines (39 loc) · 1.9 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
using System;
using static System.StringComparison;
namespace ToSic.Razor.Blade
{
public static partial class Text
{
/// <summary>
/// Get the text between two keys
/// </summary>
/// <param name="value">The initial string. If null, will always return null</param>
/// <param name="before">The key to find in front of what we're looking for. If null, will always return null</param>
/// <param name="after">The key to find in front of what we're looking for. If null, will always return null</param>
/// <param name="goToEndIfEndNotFound">If true and the end isn't found, it will return the rest of the string</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 Between(this string value, string before, string after, bool goToEndIfEndNotFound = false, bool caseSensitive = false)
=> value.BetweenInternal(before, after, goToEndIfEndNotFound, null, caseSensitive ? Ordinal : InvariantCultureIgnoreCase);
/// <summary>
/// Get string value between [first] a and [last] b.
/// </summary>
private static string BetweenInternal(
this string value,
string before,
string after,
bool goToEndIfEndNotFound = false,
string fallback = null,
StringComparison comparison = InvariantCultureIgnoreCase
)
{
if (value == null || before == null || after == null) return fallback;
var trimStart = value.AfterInternal(before, comparison);
if (trimStart == null) return fallback;
var result = trimStart.BeforeInternal(after, comparison);
if (result == null && goToEndIfEndNotFound) return trimStart;
return result;
}
}
}