/
ExtractString.cs
84 lines (71 loc) · 3.26 KB
/
ExtractString.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Description: C# Extension Methods | Enhance the .NET Framework and .NET Core with over 1000 extension methods.
// Website & Documentation: https://csharp-extension.com/
// Issues: https://github.com/zzzprojects/Z.ExtensionMethods/issues
// License (MIT): https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE
// More projects: https://zzzprojects.com/
// Copyright © ZZZ Projects Inc. All rights reserved.
using System.Text;
public static partial class Extensions
{
/// <summary>A StringBuilder extension method that extracts the string described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <returns>The extracted string.</returns>
public static StringBuilder ExtractString(this StringBuilder @this)
{
return @this.ExtractString(0);
}
/// <summary>A StringBuilder extension method that extracts the string described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted string.</returns>
public static StringBuilder ExtractString(this StringBuilder @this, out int endIndex)
{
return @this.ExtractString(0, out endIndex);
}
/// <summary>A StringBuilder extension method that extracts the string described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <returns>The extracted string.</returns>
public static StringBuilder ExtractString(this StringBuilder @this, int startIndex)
{
int endIndex;
return @this.ExtractString(startIndex, out endIndex);
}
/// <summary>A StringBuilder extension method that extracts the string described by @this.</summary>
/// <param name="this">The @this to act on.</param>
/// <param name="startIndex">The start index.</param>
/// <param name="endIndex">[out] The end index.</param>
/// <returns>The extracted string.</returns>
public static StringBuilder ExtractString(this StringBuilder @this, int startIndex, out int endIndex)
{
if (@this.Length > startIndex + 1)
{
var ch1 = @this[startIndex];
var ch2 = @this[startIndex + 1];
if (ch1 == '@' && ch2 == '"')
{
// @"my string"
return @this.ExtractStringArobasDoubleQuote(startIndex, out endIndex);
}
if (ch1 == '@' && ch2 == '\'')
{
// WARNING: This is not a valid string, however single quote is often used to make it more readable in text templating
// @'my string'
return @this.ExtractStringArobasSingleQuote(startIndex, out endIndex);
}
if (ch1 == '"')
{
// "my string"
return @this.ExtractStringDoubleQuote(startIndex, out endIndex);
}
if (ch1 == '\'')
{
// WARNING: This is not a valid string, however single quote is often used to make it more readable in text templating
// 'my string'
return @this.ExtractStringSingleQuote(startIndex, out endIndex);
}
}
endIndex = -1;
return null;
}
}