-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
74 lines (68 loc) · 2.54 KB
/
StringExtensions.cs
File metadata and controls
74 lines (68 loc) · 2.54 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
namespace CodeBuilder;
/// <summary>
/// A static class to provide extension methods for string manipulation when generating code.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Concatenates strings
/// </summary>
/// <param name="strings">List of strings to concatenate.</param>
/// <param name="joinWith">String to use between the provided list of strings when concatenating.</param>
/// <returns>Concatenated string</returns>
public static string JoinStrings(this IEnumerable<string> strings, string joinWith)
{
_ = strings ?? throw new ArgumentNullException(nameof(strings));
var combinedString = string.Empty;
IEnumerable<string> enumerable = strings.ToList();
var count = enumerable.Count();
for (var i = 0; i < count; i++)
{
combinedString += enumerable.ElementAt(i);
if (i != count - 1) combinedString += joinWith;
}
return combinedString;
}
/// <summary>
/// Wraps a string with another string as prefix and postfix.
/// <example>"variable".Wrap("'") returns 'variable'.</example>
/// </summary>
/// <param name="str">String to wrap.</param>
/// <param name="wrapWith">String to wrap with.</param>
/// <returns>Wrapped string.</returns>
public static string Wrap(this string str, string wrapWith)
{
return str.Wrap(wrapWith, wrapWith);
}
/// <summary>
/// Wraps a string with the provided prefix and suffix.
/// <example>"variable".Wrap("(",")") returns "(variable)"</example>
/// </summary>
/// <param name="str">String to wrap.</param>
/// <param name="prefix">Prefix</param>
/// <param name="suffix">Suffix</param>
/// <returns>Concatenated string with the prefix and suffix.</returns>
public static string Wrap(this string str, string prefix, string suffix)
{
_ = str ?? throw new ArgumentNullException(str);
return prefix + str + suffix;
}
/// <summary>
/// Wraps the provided string with the quotation marks.
/// </summary>
/// <param name="str">String to wrap.</param>
/// <returns>Concatenated string.</returns>
public static string WrapQuotation(this string str)
{
return str.Wrap("\"");
}
/// <summary>
/// Wraps the provided string with parenthesis.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string WrapParenthesis(this string str)
{
return str.Wrap("(", ")");
}
}