-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringExtensions.cs
197 lines (183 loc) · 7.56 KB
/
StringExtensions.cs
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
// *****************************************************
// EXTENSIONS
// by Shane Whitehead
// bwakabats@gmail.com
// *****************************************************
// The software is released under the GNU GPL:
// http://www.gnu.org/licenses/gpl.txt
//
// Feel free to use, modify and distribute this software
// I only ask you to keep this comment intact.
// Please contact me with bugs, ideas, modification etc.
// *****************************************************
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace BWakaBats.Extensions
{
public static partial class StringExtensions
{
/// <summary>
/// Returns a new string in which all occurrences of a specified string (optionally ignoring the case) in the current
/// instance are replaced with another specified string.
/// </summary>
/// <param name="source">The source string</param>
/// <param name="oldValue">The string to be replaced.</param>
/// <param name="newValue">The string to replace all occurrences of oldValue.</param>
/// <param name="ignoreCase">Ignoe the case?</param>
/// <returns>
/// A string that is equivalent to the current string except that all instances of
/// oldValue are replaced with newValue. If oldValue is not found in the current
/// instance, the method returns the current instance unchanged.
/// </returns>
public static string Replace(this string source, string oldValue, string newValue, bool ignoreCase)
{
if (!ignoreCase)
return source.Replace(oldValue, newValue);
return Regex.Replace(source, oldValue, newValue ?? "", RegexOptions.IgnoreCase);
}
/// <summary>
/// Returns a new string in which all the uppercase letter and number have been prefixed with a space.
/// </summary>
/// <param name="source">The source string</param>
/// <param name="fromVariableName">If true, then some words are removed (e.g. Id)</param>
/// <returns>The words</returns>
public static string ToWords(this string source, bool fromVariableName = false)
{
if (string.IsNullOrWhiteSpace(source))
return "";
string words = Regex.Replace(source, "[^A-Za-z0-9]", " ");
words = Regex.Replace(words, "(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])", " $1") + " ";
if (fromVariableName)
{
if (words.EndsWith(" Id ", StringComparison.Ordinal))
{
words = words.Substring(0, words.Length - 3);
}
words = words.Replace("Date Time", " ");
words = words.Replace(" Html ", " ");
}
while (words.IndexOf(" ", StringComparison.Ordinal) > 0)
{
words = words.Replace(" ", " ");
}
return words.Trim();
}
/// <summary>
/// Returns a null if the original string contains nothing but whitespace and/or HTML tags.
/// Otherwise returns the original string
/// </summary>
/// <param name="source">The source string</param>
/// <returns>The original string, or null</returns>
public static string TrimHtml(this string source)
{
//Return null if just HTML tags and whitespace...
if (string.IsNullOrWhiteSpace(source))
return null;
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
string result = source.Replace("<", "<").Replace(">", ">");
for (int index = 0; index < result.Length; index++)
{
char chr = result[index];
switch (chr)
{
case '<':
inside = true;
break;
case '>':
inside = false;
break;
default:
if (!inside)
{
array[arrayIndex] = chr;
arrayIndex++;
}
break;
}
}
result = new string(array, 0, arrayIndex);
result = result.Replace(" ", " ");
if (string.IsNullOrWhiteSpace(result))
return null;
return source; // othersize return the original source
}
/// <summary>
/// Returns the Soundex of a string. Optionally, pad the result with 0s to 4 characters
/// </summary>
/// <param name="words">The source word</param>
/// <param name="pad">Pad with 0s to 4 characters?</param>
/// <returns>The soundex</returns>
public static string Soundex(this string words, bool pad)
{
return words.Soundex(4, pad);
}
/// <summary>
/// Returns the Soundex of a string.
/// </summary>
/// <param name="words">The source work</param>
/// <param name="length">The maximum length of the Soundex</param>
/// <param name="pad">Pad the result with 0s to the maximum length?</param>
/// <returns></returns>
public static string Soundex(this string words, int length = 4, bool pad = true)
{
if (string.IsNullOrWhiteSpace(words))
return "";
string result = "";
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
string lookup = "01230120022455012623010202";
char previousChar = ' ';
char currentChar = ' ';
int currentLength = 0;
foreach (var chr in words.ToUpperInvariant())
{
if (chr >= 'A' && chr <= 'Z')
{
if (currentLength == 0)
{
result += chr;
currentLength++;
if (currentLength == length)
return result;
}
else
{
currentChar = lookup[chr - 'A'];
if (currentChar != '0' && previousChar != currentChar)
{
result += currentChar;
currentLength++;
if (currentLength == length)
return result;
}
}
}
else
{
currentChar = ' ';
}
previousChar = currentChar;
}
if (!pad)
return result;
return result + new string('0', length - currentLength);
}
/// <summary>
/// Return the string converted to a Guid
/// </summary>
/// <param name="source">The source string</param>
/// <returns>The guid equivalent</returns>
public static Guid ToGuid(this string source)
{
byte[] stringbytes = Encoding.UTF8.GetBytes(source);
using (var provider = new System.Security.Cryptography.SHA1CryptoServiceProvider())
{
byte[] hashedBytes = provider.ComputeHash(stringbytes);
Array.Resize(ref hashedBytes, 16);
return new Guid(hashedBytes);
}
}
}
}