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
117 lines (101 loc) · 3.2 KB
/
StringExtensions.cs
File metadata and controls
117 lines (101 loc) · 3.2 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
using System;
namespace JavaScriptEngineSwitcher.Core.Extensions
{
/// <summary>
/// Extensions for String
/// </summary>
public static class StringExtensions
{
/// <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>true if the quoted value occurs within this string; otherwise, false</returns>
public static bool ContainsQuotedValue(this string source, string value)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (value == 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 == null)
{
throw new ArgumentNullException(nameof(source));
}
if (trimString == 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>
public static string[] SplitToLines(this string source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
string[] result = source.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
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>true if the character was received successfully; otherwise, false</returns>
internal static bool TryGetChar(this string source, int index, out char result)
{
if (source == 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;
}
}
}