Repository navigation
Expand file tree
/
Copy pathStringToExtensions.cs
More file actions
359 lines (308 loc) · 14.1 KB
/
Copy pathStringToExtensions.cs
File metadata and controls
359 lines (308 loc) · 14.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace ByteDev.Strings
{
/// <summary>
/// Extension methods for <see cref="T:System.String" />.
/// </summary>
public static class StringToExtensions
{
private static readonly HashSet<string> ExtendedTrueSet = new HashSet<string>
{
"1", "Y", "y", "on", "On", "ON", "oN"
};
/// <summary>
/// Returns the string as a <typeparamref name="TEnum" />.
/// </summary>
/// <typeparam name="TEnum">Enum type to return.</typeparam>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="ignoreCase">True to ignore the string's case; false to regard case.</param>
/// <returns>The string as a <typeparamref name="TEnum" />.</returns>
/// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
/// <exception cref="T:System.ArgumentException"><paramref name="source" /> is not a valid enum value.</exception>
public static TEnum ToEnum<TEnum>(this string source, bool ignoreCase = false) where TEnum : Enum
{
return (TEnum)Enum.Parse(typeof(TEnum), source, ignoreCase);
}
/// <summary>
/// Returns a copy of the string converted to title case.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>The string in title case.</returns>
public static string ToTitleCase(this string source)
{
if (source == null)
return null;
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(source.ToLower());
}
/// <summary>
/// Returns this string as a sequence of lines.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="ignoreEmptyLines">If true empty and whitespace only lines will not be returned.</param>
/// <returns>The string as a sequence of lines.</returns>
public static IEnumerable<string> ToLines(this string source, bool ignoreEmptyLines = false)
{
if (source == null)
source = string.Empty;
using (var reader = new StringReader(source))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (ignoreEmptyLines && string.IsNullOrWhiteSpace(line))
continue;
yield return line;
}
}
}
/// <summary>
/// Returns this string as a list of lines.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="ignoreEmptyLines">If true empty and whitespace only lines will not be returned.</param>
/// <returns>The string as a list of lines.</returns>
public static IList<string> ToLinesList(this string source, bool ignoreEmptyLines = false)
{
if (string.IsNullOrEmpty(source))
return new List<string>();
var lines = new List<string>();
using (var reader = new StringReader(source))
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (ignoreEmptyLines && string.IsNullOrWhiteSpace(line))
continue;
lines.Add(line);
}
}
return lines;
}
/// <summary>
/// Returns this string as a byte array.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>The string as an array of bytes.</returns>
public static byte[] ToByteArray(this string source)
{
return ToByteArray(source, new UTF8Encoding());
}
/// <summary>
/// Returns this string as a byte array using the given <paramref name="encoding" />.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="encoding">Byte array encoding.</param>
/// <returns>The string as an array of bytes.</returns>
public static byte[] ToByteArray(this string source, Encoding encoding)
{
if (string.IsNullOrEmpty(source))
return Array.Empty<byte>();
return encoding.GetBytes(source);
}
/// <summary>
/// Returns the string as a bool. Will attempt to determine if the string represents true
/// based on an extended set of case insensitive invariant possible values:
/// { true, 1, y, on, yes, enable, enabled }
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>The string as a bool if valid true; otherwise false.</returns>
public static bool ToBoolExtended(this string source)
{
if (source == null)
return false;
if (bool.TryParse(source, out var result))
return result;
if (ExtendedTrueSet.Contains(source))
return true;
if (source.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
return true;
if (source.Equals("enable", StringComparison.InvariantCultureIgnoreCase))
return true;
if (source.Equals("enabled", StringComparison.InvariantCultureIgnoreCase))
return true;
return false;
}
/// <summary>
/// Returns the string as a bool.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>The string as a bool if valid true; otherwise false.</returns>
public static bool ToBool(this string source)
{
if (bool.TryParse(source, out var result))
return result;
return false;
}
/// <summary>
/// Returns this string as a nullable bool.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="defaultValue">Default value to return if unable to convert to nullable bool.</param>
/// <returns>The string as a nullable bool if valid true; otherwise default value.</returns>
public static bool? ToBoolOrDefault(this string source, bool? defaultValue = null)
{
return bool.TryParse(source, out var result) ? result : defaultValue;
}
/// <summary>
/// Returns string as an Int32.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>The string as a Int32 if valid; otherwise zero.</returns>
public static int ToInt32(this string source)
{
return int.TryParse(source, out var result) ? result : 0;
}
/// <summary>
/// Returns this string as a nullable Int32.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="defaultValue">Default value to return if unable to convert to nullable int.</param>
/// <returns>The string as a nullable Int32 if valid; otherwise default value.</returns>
public static int? ToInt32OrDefault(this string source, int? defaultValue = null)
{
return int.TryParse(source, out var result) ? result : defaultValue;
}
/// <summary>
/// Returns string as a Int64.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>The string as a Int64 if valid; otherwise zero.</returns>
public static long ToInt64(this string source)
{
return long.TryParse(source, out var result) ? result : 0;
}
/// <summary>
/// Returns string as a nullable Int64.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="defaultValue">Default value to return if unable to convert to nullable long.</param>
/// <returns>The string as a nullable long if valid; otherwise default value.</returns>
public static long? ToInt64OrDefault(this string source, long? defaultValue = null)
{
return long.TryParse(source, out var result) ? result : defaultValue;
}
/// <summary>
/// Returns the string as a sequence with each value determined by the specified char delimiter.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="delimiter">Value delimiter.</param>
/// <param name="trimValues">True trim each value; false do nothing.</param>
/// <returns>Collection of values; otherwise empty.</returns>
public static IEnumerable<string> ToSequence(this string source, char delimiter, bool trimValues = false)
{
if (string.IsNullOrEmpty(source))
return Enumerable.Empty<string>();
string[] parts = source.Split(delimiter);
if (!trimValues)
return parts;
return parts.Select(a => a.Trim()).Where(s => s != string.Empty);
}
/// <summary>
/// Returns the string as a sequence with each value determined by the specified string delimiter.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="delimiter">Value delimiter.</param>
/// <param name="trimValues">True trim each value; false do nothing.</param>
/// <returns>Collection of values; otherwise empty.</returns>
public static IEnumerable<string> ToSequence(this string source, string delimiter, bool trimValues = false)
{
return ToSequence(source, new[] {delimiter}, trimValues);
}
/// <summary>
/// Returns the string as a sequence with each value determined by the specified array of string delimiters.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="delimiters">Value delimiter.</param>
/// <param name="trimValues">True trim each value; false do nothing.</param>
/// <returns>Collection of values; otherwise empty.</returns>
public static IEnumerable<string> ToSequence(this string source, string[] delimiters, bool trimValues = false)
{
if (string.IsNullOrEmpty(source))
return Enumerable.Empty<string>();
string[] parts = source.Split(delimiters, StringSplitOptions.None);
if (!trimValues)
return parts;
return parts.Select(a => a.Trim()).Where(s => s != string.Empty);
}
/// <summary>
/// Returns the string as a Uri.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>New Uri instance if the string is a Uri; otherwise null.</returns>
public static Uri ToUri(this string source)
{
if (string.IsNullOrEmpty(source))
return null;
try
{
return new Uri(source);
}
catch (UriFormatException)
{
return null;
}
}
/// <summary>
/// Returns the string as a Guid.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>New Guid instance if the string is a Guid; otherwise default.</returns>
public static Guid ToGuid(this string source)
{
if (source == null)
return default;
try
{
return new Guid(source);
}
catch (FormatException)
{
return default;
}
}
/// <summary>
/// Returns the string as a KeyValuePair.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="delimiter">String delimiter for the key value pair.</param>
/// <returns>New KeyValuePair instance.</returns>
public static KeyValuePair<string, string> ToKeyValuePair(this string source, string delimiter)
{
if (source == null)
return default;
if (string.IsNullOrEmpty(delimiter))
return new KeyValuePair<string, string>(source, string.Empty);
int index = source.IndexOf(delimiter, StringComparison.Ordinal);
if (index < 0)
return new KeyValuePair<string, string>(source, string.Empty);
var key = source.Substring(0, index);
var value = source.Substring(index + delimiter.Length);
return new KeyValuePair<string, string>(key, value);
}
/// <summary>
/// Returns the string as a MemoryStream using UTF-8 encoding.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <returns>New MemoryStream instance.</returns>
public static MemoryStream ToMemoryStream(this string source)
{
return ToMemoryStream(source, Encoding.UTF8);
}
/// <summary>
/// Returns the string as a MemoryStream.
/// </summary>
/// <param name="source">The string to perform this operation on.</param>
/// <param name="encoding">Encoding to use.</param>
/// <returns>New MemoryStream instance.</returns>
public static MemoryStream ToMemoryStream(this string source, Encoding encoding)
{
byte[] bytes = encoding.GetBytes(source ?? string.Empty);
return new MemoryStream(bytes);
}
}
}