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
|
using System;
using System.Collections.Generic;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Contains <see cref="String"/> extension methods.
/// </summary>
public static class StringExtensions
{
private static Regex titleRegEx;
/// <summary>
/// Initializes the <see cref="StringExtensions"/> class.
/// </summary>
static StringExtensions()
{
titleRegEx = new Regex(@"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
}
/// <summary>
/// Converts the object to string. If the object is null, will return null.
/// </summary>
/// <param name="obj">The object.</param>
/// <returns></returns>
public static String ToStringSafe(this object obj)
{
return obj != null ? obj.ToString() : String.Empty;
}
/// <summary>
/// Splits the string to lines.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static List<String> ToLines(this String str)
{
//return str.Split(new[] { '\r', '\n' }).ToList();
if (str == null) return new List<string>();
return str.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None).ToList();
}
/// <summary>
/// Formats the string as title style.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static String ToTitle(this String str)
{
return titleRegEx.Replace(str, " ");
}
/// <summary>
/// Formats the string as title case.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static String ToTitleCase(this String str)
{
return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}
/// <summary>
/// Converts the specified database conventional name to the observables conventional name.
/// </summary>
/// <param name="dalName">DAL name.</param>
/// <returns></returns>
public static String FromDalNameToTitleCase(this String dalName)
{
return String.Join("", dalName.Split('_').Select(x => ToTitleCase(x)));
}
/// <summary>
/// Singularizes the string.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static String SingularizeMVC(this String text)
{
var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
return serv.Singularize(text);
}
/// <summary>
/// Pluralizes the string.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static String PluralizeMVC(this String text)
{
var serv = PluralizationService.CreateService(new System.Globalization.CultureInfo("en-us"));
return serv.Pluralize(text);
}
/// <summary>
/// Truncates the specified string to the specified max length and appends ellipsis.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="maxLength">Max length</param>
/// <returns></returns>
public static String Ellipsis(this String text, int maxLength)
{
return text.Length <= maxLength ? text : text.Substring(0, maxLength) + "...";
}
/// <summary>
/// Converts the string to camel case string.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static String ToCamelCase(this String str)
{
if (String.IsNullOrEmpty(str) || Char.IsLower(str, 0))
return str;
return Char.ToLowerInvariant(str[0]) + str.Substring(1);
}
/// <summary>
/// Compares the two strings based on letter pair matches
/// </summary>
/// <param name="str1"></param>
/// <param name="str2"></param>
/// <returns>The percentage match from 0.0 to 1.0 where 1.0 is 100%</returns>
public static double CompareSimilarity(this string str1, string str2)
{
List<string> pairs1 = WordLetterPairs(str1.ToUpper());
List<string> pairs2 = WordLetterPairs(str2.ToUpper());
int intersection = 0;
int union = pairs1.Count + pairs2.Count;
for (int i = 0; i < pairs1.Count; i++)
{
for (int j = 0; j < pairs2.Count; j++)
{
if (pairs1[i] == pairs2[j])
{
intersection++;
pairs2.RemoveAt(j);//Must remove the match to prevent "GGGG" from appearing to match "GG" with 100% success
break;
}
}
}
return (2.0 * intersection) / union;
}
/// <summary>
/// Gets all letter pairs for each
/// individual word in the string
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static List<string> WordLetterPairs(string str)
{
List<string> AllPairs = new List<string>();
// Tokenize the string and put the tokens/words into an array
string[] Words = Regex.Split(str, @"\s");
// For each word
for (int w = 0; w < Words.Length; w++)
{
if (!string.IsNullOrEmpty(Words[w]))
{
// Find the pairs of characters
String[] PairsInWord = LetterPairs(Words[w]);
for (int p = 0; p < PairsInWord.Length; p++)
{
AllPairs.Add(PairsInWord[p]);
}
}
}
return AllPairs;
}
/// <summary>
/// Generates an array containing every
/// two consecutive letters in the input string
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string[] LetterPairs(string str)
{
int numPairs = str.Length - 1;
string[] pairs = new string[numPairs];
for (int i = 0; i < numPairs; i++)
{
pairs[i] = str.Substring(i, 2);
}
return pairs;
}
/// <summary>
/// Splits the camel case sentence to words.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static string ToWords(this string str)
{
return Regex.Replace(
Regex.Replace(
str,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
);
}
/// <summary>
/// Removes any invalid file name characters from the string.
/// </summary>
/// <param name="str">The string.</param>
/// <returns></returns>
public static string ToValidFileName(this string str)
{
char[] _invalidChars = System.IO.Path.GetInvalidFileNameChars();
String validFileName = str;
if (validFileName != null)
{
foreach (var c in _invalidChars)
{
validFileName = validFileName.Replace(c.ToString(), "");
}
}
return validFileName;
}
public static String ToStringOrEmpty(this String str)
{
return str != null ? str : String.Empty;
}
public static String ToNullIfEmpty(this String str)
{
return String.IsNullOrEmpty(str) ? null : str;
}
public static bool IsNotNullOrEmpty(this String str)
{
return !String.IsNullOrWhiteSpace(str);
}
public static String ToOneLine(this String str)
{
return str.Replace(Environment.NewLine, " ");
}
public static List<T> ToEnumValues<T>(this String str, char splitChar) where T : struct
{
if (!String.IsNullOrWhiteSpace(str))
{
return str.Split(splitChar).Select(x => (T)(object)int.Parse(x)).ToList();
}
else
{
return new List<T>();
}
}
public static IEnumerable<int> AllIndexesOf(this string str, string searchstring)
{
int minIndex = str.IndexOf(searchstring);
while (minIndex != -1)
{
yield return minIndex;
minIndex = str.IndexOf(searchstring, minIndex + searchstring.Length);
}
}
}
|