-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHelper.cs
More file actions
244 lines (205 loc) · 9.69 KB
/
StringHelper.cs
File metadata and controls
244 lines (205 loc) · 9.69 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
// <copyright file="StringHelper.cs" company="SoluiNet">
// Copyright (c) SoluiNet. All rights reserved.
// </copyright>
namespace SoluiNet.Core.Tools.String
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
/// <summary>
/// Provides a collection of methods which support working with strings.
/// </summary>
public static class StringHelper
{
/// <summary>
/// Replace the first occurence of a RegEx-pattern.
/// </summary>
/// <param name="inputString">The string in which should be searched.</param>
/// <param name="searchPattern">The search pattern (RegEx).</param>
/// <param name="replacementValue">The replacement value. If not overgiven an empty string will be used.</param>
/// <returns>The <paramref name="inputString"/> in which the first occurence of <paramref name="searchPattern"/> has been replaced with <paramref name="replacementValue"/>.</returns>
public static string ReplaceFirstOccurence(string inputString, string searchPattern, string replacementValue = "")
{
var regEx = new Regex(Regex.Escape(searchPattern));
return regEx.Replace(inputString, replacementValue, 1);
}
/// <summary>
/// Prepare the header label of visual components.
/// </summary>
/// <param name="headerLabel">The header label.</param>
/// <param name="removeEntityStructure">A value which indicated if the entity structure should be removed. Only the last entity name will be used.</param>
/// <returns>Returns a <see cref="string"/> which will be displayed correctly in the header of a visual component.</returns>
public static string PrepareHeaderLabel(string headerLabel, bool removeEntityStructure = true)
{
if (headerLabel == null)
{
throw new ArgumentNullException(nameof(headerLabel));
}
var headerLabelString = headerLabel;
if (headerLabelString.Contains('.') && removeEntityStructure)
{
var positionOfLastDot = headerLabelString.LastIndexOf('.');
headerLabelString = headerLabelString.Substring(positionOfLastDot + 1, headerLabelString.Length - positionOfLastDot - 1);
}
return headerLabelString.Replace("_", "__");
}
/// <summary>
/// Add line numbers to a string.
/// </summary>
/// <param name="originalString">The string which should be extended with line numbers.</param>
/// <param name="numberOfDigits">The number of digits which should be used for the line number.</param>
/// <returns>Returns a <see cref="string"/> which represents the <paramref name="originalString"/> with line numbers at the beginning of each line.</returns>
public static string AddLineNumbers(this string originalString, int numberOfDigits = 2)
{
if (originalString == null)
{
throw new ArgumentNullException(nameof(originalString));
}
var temporaryString = string.Empty;
var lineNumber = 1;
foreach (var line in originalString.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.None))
{
temporaryString += string.Format(CultureInfo.InvariantCulture, "{0}: {1}\r\n", lineNumber++.ToString("D" + numberOfDigits.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture), line);
}
return temporaryString.Remove(temporaryString.Length - 2, 2);
}
/// <summary>
/// Convert a string to a base64 value.
/// </summary>
/// <param name="originalString">The string which should be casted to base64.</param>
/// <returns>A <see cref="string"/> which represents the base64 value for <paramref name="originalString"/>.</returns>
public static string ToBase64(this string originalString)
{
var plainTextBytes = Encoding.UTF8.GetBytes(originalString);
return Convert.ToBase64String(plainTextBytes);
}
/// <summary>
/// Convert a string from base64 to its' original value.
/// </summary>
/// <param name="encodedString">The string which should be converted from base64.</param>
/// <returns>A <see cref="string"/> which represents the original value for <paramref name="encodedString"/>.</returns>
public static string FromBase64(this string encodedString)
{
byte[] data = Convert.FromBase64String(encodedString);
return Encoding.UTF8.GetString(data);
}
/// <summary>
/// Get the number of seconds which are represented by the duration string.
/// </summary>
/// <param name="durationString">The duration string.</param>
/// <returns>Returns the number of seconds which the duration string represents.</returns>
public static long GetSecondsFromDurationString(this string durationString)
{
long duration = 0;
var durationRegEx = new Regex(@"((\d+)w)?\s*((\d+)d)?\s*((\d+)h)?\s*((\d+)m)?\s*((\d+)s?)");
var match = durationRegEx.Match(durationString);
if (match.Success)
{
if (match.Groups[2].Success)
{
duration += Convert.ToInt32(match.Groups[2].Value, CultureInfo.InvariantCulture) * 7 * 24 * 60 * 60;
}
if (match.Groups[4].Success)
{
duration += Convert.ToInt32(match.Groups[4].Value, CultureInfo.InvariantCulture) * 24 * 60 * 60;
}
if (match.Groups[6].Success)
{
duration += Convert.ToInt32(match.Groups[6].Value, CultureInfo.InvariantCulture) * 60 * 60;
}
if (match.Groups[8].Success)
{
duration += Convert.ToInt32(match.Groups[8].Value, CultureInfo.InvariantCulture) * 60;
}
if (match.Groups[10].Success)
{
duration += Convert.ToInt32(match.Groups[10].Value, CultureInfo.InvariantCulture);
}
}
return duration;
}
/// <summary>
/// Check if a regular expression matches an overgiven string.
/// </summary>
/// <param name="regExPattern">The regular expression.</param>
/// <param name="searchString">The string which should be checked.</param>
/// <returns>Returns true if the search string matches the regular expression.</returns>
public static bool RegExMatch(this string regExPattern, string searchString)
{
if (string.IsNullOrEmpty(regExPattern) || string.IsNullOrEmpty(searchString))
{
return false;
}
var regEx = new Regex(regExPattern);
return regEx.IsMatch(searchString);
}
/// <summary>
/// Check if a regular expression matches an overgiven string.
/// </summary>
/// <param name="searchString">The string which should be checked.</param>
/// <param name="regExPattern">The regular expression.</param>
/// <returns>Returns true if the search string matches the regular expression.</returns>
public static bool MatchesRegEx(this string searchString, string regExPattern)
{
if (string.IsNullOrEmpty(regExPattern) || string.IsNullOrEmpty(searchString))
{
return false;
}
var regEx = new Regex(regExPattern);
return regEx.IsMatch(searchString);
}
/// <summary>
/// Check if a regular expression matches an overgiven string.
/// </summary>
/// <param name="originalString">The string which should be replaced.</param>
/// <param name="regExPattern">The regular expression.</param>
/// <param name="replacementString">The replacement string.</param>
/// <returns>The string with replacement.</returns>
public static string ReplaceRegEx(this string originalString, string regExPattern, string replacementString = "")
{
if (string.IsNullOrEmpty(regExPattern) || string.IsNullOrEmpty(originalString))
{
return originalString;
}
var regEx = new Regex(regExPattern);
return regEx.Replace(originalString, replacementString);
}
/// <summary>
/// Returns true if the string has an affirmative value (1 / true / wahr / y / yes).
/// </summary>
/// <param name="stringValue">The string value.</param>
/// <returns>If the string represents an affirmative value return true, otherwise false.</returns>
public static bool IsAffirmative(this string stringValue)
{
if (stringValue == null)
{
throw new ArgumentNullException(nameof(stringValue));
}
if (stringValue.ToUpperInvariant() == "1")
{
return true;
}
if (stringValue.ToUpperInvariant() == "TRUE")
{
return true;
}
if (stringValue.ToUpperInvariant() == "WAHR")
{
return true;
}
if (stringValue.ToUpperInvariant() == "Y")
{
return true;
}
if (stringValue.ToUpperInvariant() == "YES")
{
return true;
}
return false;
}
}
}