forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
144 lines (123 loc) · 4.06 KB
/
StringExtensions.cs
File metadata and controls
144 lines (123 loc) · 4.06 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
namespace JavaScriptEngineSwitcher.Core.Extensions
{
/// <summary>
/// Extensions for String
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Array of strings used to find the newline
/// </summary>
private static readonly string[] _newLineStrings = ["\r\n", "\r", "\n"];
/// <summary>
/// Returns a value indicating whether the specified quoted string occurs within this string
/// </summary>
/// <param name="source">Instance of <see cref="String"/></param>
/// <param name="value">The string without quotes to seek</param>
/// <returns><c>true</c> if the quoted value occurs within this string; otherwise, <c>false</c></returns>
public static bool ContainsQuotedValue(this string source, string value)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
bool result = source.Contains("'" + value + "'") || source.Contains("\"" + value + "\"");
return result;
}
/// <summary>
/// Removes leading occurrence of the specified string from the current <see cref="String"/> object
/// </summary>
/// <param name="source">Instance of <see cref="String"/></param>
/// <param name="trimString">An string to remove</param>
/// <returns>The string that remains after removing of the specified string from the start of
/// the current string</returns>
public static string TrimStart(this string source, string trimString)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (trimString is null)
{
throw new ArgumentNullException(nameof(trimString));
}
if (source.Length == 0 || trimString.Length == 0)
{
return source;
}
string result = source;
if (source.StartsWith(trimString, StringComparison.Ordinal))
{
result = source.Substring(trimString.Length);
}
return result;
}
/// <summary>
/// Splits a string into lines
/// </summary>
/// <param name="source">Instance of <see cref="String"/></param>
/// <returns>An array of lines</returns>
[Obsolete]
public static string[] SplitToLines(this string source)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
string[] result = source.Split(_newLineStrings, StringSplitOptions.None);
return result;
}
/// <summary>
/// Splits a string into lines
/// </summary>
/// <param name="source">Instance of <see cref="String"/></param>
/// <param name="options"><see cref="StringSplitOptions.RemoveEmptyEntries"/> to omit empty array
/// elements from the array returned; or <see cref="StringSplitOptions.None"/> to include empty
/// array elements in the array returned</param>
/// <returns>An array of lines</returns>
internal static string[] SplitToLines(this string source, StringSplitOptions options)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
string[] result = source.Split(_newLineStrings, options);
return result;
}
/// <summary>
/// Gets a character at the specified index from the string.
/// A return value indicates whether the receiving succeeded.
/// </summary>
/// <param name="source">The source string</param>
/// <param name="index">The zero-based index of the character</param>
/// <param name="result">When this method returns, contains the character from the string,
/// if the receiving succeeded, or null character if the receiving failed.
/// The receiving fails if the index out of bounds.</param>
/// <returns><c>true</c> if the character was received successfully; otherwise, <c>false</c></returns>
internal static bool TryGetChar(this string source, int index, out char result)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
bool isSuccess;
int length = source.Length;
if (length > 0 && index >= 0 && index < length)
{
result = source[index];
isSuccess = true;
}
else
{
result = '\0';
isSuccess = false;
}
return isSuccess;
}
}
}