2
using System.Collections.Generic;
3
using System.Globalization;
5
using System.Text.RegularExpressions;
6
using ServiceStack.Common.Utils;
7
using ServiceStack.Text;
8
using ServiceStack.Text.Common;
10
namespace ServiceStack.Common
12
public static class StringExtensions
14
static readonly Regex RegexSplitCamelCase = new Regex("([A-Z]|[0-9]+)",
15
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
22
public static T ToEnum<T>(this string value)
24
return (T)Enum.Parse(typeof(T), value, true);
27
public static T ToEnumOrDefault<T>(this string value, T defaultValue)
29
if (String.IsNullOrEmpty(value)) return defaultValue;
30
return (T)Enum.Parse(typeof(T), value, true);
33
public static string SplitCamelCase(this string value)
35
return RegexSplitCamelCase.Replace(value, " $1").TrimStart();
38
public static string ToEnglish(this string camelCase)
40
var ucWords = camelCase.SplitCamelCase().ToLower();
41
return ucWords[0].ToString(CultureInfo.InvariantCulture).ToUpper() + ucWords.Substring(1);
44
public static bool IsEmpty(this string value)
46
return String.IsNullOrEmpty(value);
49
public static bool IsNullOrEmpty(this string value)
51
return String.IsNullOrEmpty(value);
54
public static bool EqualsIgnoreCase(this string value, string other)
56
return String.Equals(value, other, StringComparison.CurrentCultureIgnoreCase);
59
public static string ReplaceFirst(this string haystack, string needle, string replacement)
61
var pos = haystack.IndexOf(needle);
62
if (pos < 0) return haystack;
64
return haystack.Substring(0, pos) + replacement + haystack.Substring(pos + needle.Length);
67
public static string ReplaceAll(this string haystack, string needle, string replacement)
70
// Avoid a possible infinite loop
71
if (needle == replacement) return haystack;
72
while ((pos = haystack.IndexOf(needle)) > 0)
74
haystack = haystack.Substring(0, pos)
76
+ haystack.Substring(pos + needle.Length);
81
public static bool ContainsAny(this string text, params string[] testMatches)
83
foreach (var testMatch in testMatches)
85
if (text.Contains(testMatch)) return true;
90
private static readonly Regex InvalidVarCharsRegEx = new Regex(@"[^A-Za-z0-9]",
91
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
98
public static string SafeVarName(this string text)
100
if (String.IsNullOrEmpty(text)) return null;
101
return InvalidVarCharsRegEx.Replace(text, "_");
104
public static string Join(this List<string> items)
106
return String.Join(JsWriter.ItemSeperatorString, items.ToArray());
109
public static string Join(this List<string> items, string delimeter)
111
return String.Join(delimeter, items.ToArray());
114
public static string CombineWith(this string path, params string[] thesePaths)
116
if (thesePaths.Length == 1 && thesePaths[0] == null) return path;
117
return PathUtils.CombinePaths(new StringBuilder(path.TrimEnd('/','\\')), thesePaths);
120
public static string ToParentPath(this string path)
122
var pos = path.LastIndexOf('/');
123
if (pos == -1) return "/";
125
var parentPath = path.Substring(0, pos);
129
public static string RemoveCharFlags(this string text, bool[] charFlags)
131
if (text == null) return null;
133
var copy = text.ToCharArray();
136
for (var i = 0; i < text.Length; i++)
139
if (@char < charFlags.Length && charFlags[@char]) continue;
140
copy[nonWsPos++] = @char;
143
return new String(copy, 0, nonWsPos);
146
public static string ToNullIfEmpty(this string text)
148
return String.IsNullOrEmpty(text) ? null : text;
152
private static char[] SystemTypeChars = new[] { '<', '>', '+' };
154
public static bool IsUserType(this Type type)
157
&& type.Namespace != null
158
&& !type.Namespace.StartsWith("System.")
159
&& type.Name.IndexOfAny(SystemTypeChars) == -1;
162
public static bool IsInt(this string text)
164
if (string.IsNullOrEmpty(text)) return false;
166
return int.TryParse(text, out ret);
169
public static int ToInt(this string text)
171
return int.Parse(text);
174
public static int ToInt(this string text, int defaultValue)
177
return int.TryParse(text, out ret) ? ret : defaultValue;
180
public static long ToInt64(this string text)
182
return long.Parse(text);
185
public static long ToInt64(this string text, long defaultValue)
188
return long.TryParse(text, out ret) ? ret : defaultValue;
b'\\ No newline at end of file'