Last active
February 1, 2024 21:04
-
-
Save 5up3rman/2d305a861328cf34826bd99f3a41a547 to your computer and use it in GitHub Desktop.
String Utilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sitecore.Collections; | |
using Sitecore.Diagnostics; | |
using Sitecore.Reflection; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace Sitecore | |
{ | |
/// <summary>Implements a utility class for manipulating strings.</summary> | |
/// <remarks> | |
/// <seealso cref="T:Sitecore.DateUtil" /> | |
/// <seealso cref="T:Sitecore.MainUtil" /> | |
/// <seealso cref="T:Sitecore.StringUtil" /> | |
/// </remarks> | |
public sealed class StringUtil | |
{ | |
private static readonly Regex m_removeTagsRegex = new Regex("<[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Compiled); | |
/// <summary>Arrays to string.</summary> | |
/// <param name="array">The array.</param> | |
/// <param name="delimiter">The delimiter.</param> | |
/// <returns></returns> | |
public static string ArrayToString(Array array, char delimiter) | |
{ | |
Assert.ArgumentNotNull((object) array, "array"); | |
string empty = string.Empty; | |
for (int index = 0; index < array.Length; ++index) | |
{ | |
object obj = array.GetValue(index); | |
if (obj != null) | |
{ | |
if (index > 0) | |
empty += (string) (object) delimiter; | |
string str = obj.ToString(); | |
empty += str; | |
} | |
} | |
return empty; | |
} | |
/// <summary> | |
/// Uppercases the first letter of a string and | |
/// lowercases the remaining letters. | |
/// </summary> | |
/// <param name="text">A string.</param> | |
/// <returns>The capitalized string.</returns> | |
/// <remarks>Sentences are not taken into account.</remarks> | |
/// <example> | |
/// <code> | |
/// string text = MainUtil.Capitalize("HELLO WORLD."); // "Hello world" | |
/// string text = MainUtil.Capitalize("HELLO. HOW ARE YOU?"); // "Hello. how are you?" | |
/// </code> | |
/// </example> | |
public static string Capitalize(string text) | |
{ | |
string str = text.Trim(); | |
if (str.Length == 0) | |
return string.Empty; | |
return str.Substring(0, 1).ToUpperInvariant() + str.Substring(1).ToLowerInvariant(); | |
} | |
/// <summary>Clips a string at a certain length.</summary> | |
/// <param name="text">The text that will be clipped.</param> | |
/// <param name="length">The maximum length of the returned string.</param> | |
/// <param name="ellipsis">Indicates if the string should have en ellipsis (...) appended</param> | |
/// <returns>The clipped string.</returns> | |
/// <example> | |
/// <code> | |
/// string s0 = StringUtil.Clip("Hello world", 5, false); // "Hello" | |
/// string s1 = StringUtil.Clip("Hello world", 5, true); // "He..." | |
/// </code> | |
/// </example> | |
public static string Clip(string text, int length, bool ellipsis) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertInt(length, "length", false, false); | |
text = text.Replace(" ", " ").Replace(" ", " "); | |
if (text.Length > length) | |
{ | |
if (ellipsis) | |
length -= 3; | |
int length1 = text.LastIndexOf(" ", length, StringComparison.InvariantCulture); | |
if (length1 < 0) | |
length1 = length; | |
text = text.Substring(0, length1); | |
if (ellipsis) | |
text += "..."; | |
} | |
return text; | |
} | |
/// <summary>Combines the specified parts.</summary> | |
/// <param name="part1">The part1.</param> | |
/// <param name="part2">The part2.</param> | |
/// <param name="divider">The divider.</param> | |
/// <returns></returns> | |
public static string Combine(object part1, object part2, string divider) | |
{ | |
Assert.ArgumentNotNull((object) divider, "divider"); | |
if (part1 == null || part1.ToString().Length == 0) | |
{ | |
if (part2 == null) | |
return string.Empty; | |
return part2.ToString(); | |
} | |
if (part2 == null || part2.ToString().Length == 0) | |
return part1.ToString(); | |
return part1.ToString() + divider + part2; | |
} | |
/// <summary>Concats the specified values.</summary> | |
/// <param name="values">The values.</param> | |
/// <returns></returns> | |
public static string Concat(params object[] values) | |
{ | |
return string.Concat(((IEnumerable<object>) values).Select<object, string>((Func<object, string>) (value => value.ToString())).ToArray<string>()); | |
} | |
/// <summary>Determines whether [contains] [the specified text].</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="texts">The texts.</param> | |
/// <returns> | |
/// <c>true</c> if [contains] [the specified text]; otherwise, <c>false</c>. | |
/// </returns> | |
public static bool Contains(string text, string[] texts) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertObject((object) texts, "texts"); | |
return StringUtil.IndexOf(text, texts) >= 0; | |
} | |
/// <summary>Cuts a string into evenly sized parts.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="chunkSize">Number of characters in a parts.</param> | |
/// <returns>An array list of strings.</returns> | |
/// <example> | |
/// <code> | |
/// string text = "Hello world"; | |
/// ArrayList list = StringUtil.Cut(test, 3); | |
/// // "Hel", "llo", " wo", "rld" | |
/// </code> | |
/// </example> | |
public static ArrayList CutUp(string text, int chunkSize) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertInt(chunkSize, "chunkSize", false, false); | |
ArrayList arrayList = new ArrayList(); | |
int int32; | |
for (string str = text; str.Length > 0; str = str.Substring(int32)) | |
{ | |
int32 = System.Convert.ToInt32(Math.Min(str.Length, chunkSize)); | |
arrayList.Add((object) str.Substring(0, int32)); | |
} | |
return arrayList; | |
} | |
/// <summary>Decodes the specified bytes.</summary> | |
/// <param name="bytes">The bytes.</param> | |
/// <returns></returns> | |
public static string Decode(byte[] bytes) | |
{ | |
return Encoding.UTF8.GetString(bytes); | |
} | |
/// <summary>Decodes the message parameter.</summary> | |
/// <param name="value">The value.</param> | |
/// <returns>The message parameter.</returns> | |
public static string DecodeMessageParameter(string value) | |
{ | |
return value.Replace("_eq_", "=").Replace("_qst_", "?"); | |
} | |
/// <summary>Dictionaries to name values.</summary> | |
/// <param name="dictionary">The dictionary.</param> | |
/// <returns>The to name values.</returns> | |
public static NameValueCollection DictionaryToNameValues(SafeDictionary<string> dictionary) | |
{ | |
Assert.ArgumentNotNull((object) dictionary, "dictionary"); | |
NameValueCollection nameValueCollection = new NameValueCollection(dictionary.Count); | |
foreach (KeyValuePair<string, string> keyValuePair in (SafeDictionary<string, string>) dictionary) | |
nameValueCollection.Add(keyValuePair.Key, keyValuePair.Value); | |
return nameValueCollection; | |
} | |
/// <summary> | |
/// Divides a string using a specified separator character. | |
/// </summary> | |
/// <example> | |
/// StringUtil.Divide("da-DK-kl", '-', false); // returns {"da", "DK-kl"} | |
/// StringUtil.Divide("da-DK-kl", '-', true); // returns {"da-DK", "kl"} | |
/// StringUtil.Divide("da", '-', false); // returns {"da"} | |
/// </example> | |
/// <param name="value">The value.</param> | |
/// <param name="separator">The separator.</param> | |
/// <param name="useLastSeparator">if set to <c>true</c> this instance is use last separator.</param> | |
/// <returns></returns> | |
public static string[] Divide(string value, char separator, bool useLastSeparator) | |
{ | |
Assert.ArgumentNotNull((object) value, "name"); | |
int length = useLastSeparator ? value.LastIndexOf(separator) : value.IndexOf(separator); | |
if (length >= 0) | |
return new string[2]{ StringUtil.Left(value, length), StringUtil.Mid(value, length + 1) }; | |
return new string[1]{ value }; | |
} | |
/// <summary>Divides a string in two at the first digit.</summary> | |
/// <example> | |
/// StringUtil.DivideAtDigit("da-DK12"); // returns {"da-DK", "12"} | |
/// StringUtil.DivideAtDigit("da-DK"); // returns {"da-DK"} | |
/// </example> | |
/// <param name="value">The value.</param> | |
/// <returns></returns> | |
public static string[] DivideAtDigit(string value) | |
{ | |
int num = -1; | |
for (int index = 0; index < value.Length; ++index) | |
{ | |
if (char.IsDigit(value[index])) | |
{ | |
num = index; | |
break; | |
} | |
} | |
if (num >= 0) | |
return new string[2]{ StringUtil.Left(value, num), StringUtil.Mid(value, num) }; | |
return new string[1]{ value }; | |
} | |
/// <summary>Encodes the specified text.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns></returns> | |
public static byte[] Encode(string text) | |
{ | |
return Encoding.UTF8.GetBytes(text); | |
} | |
/// <summary>Encodes the message parameter.</summary> | |
/// <param name="value">The value.</param> | |
/// <returns>The message parameter.</returns> | |
public static string EncodeMessageParameter(string value) | |
{ | |
Assert.ArgumentNotNullOrEmpty(value, "value"); | |
return value.Replace("=", "_eq_").Replace("?", "_qst_"); | |
} | |
/// <summary>Ensures that a string has a certain postfix.</summary> | |
/// <param name="postfix">The postfix.</param> | |
/// <param name="value">The value.</param> | |
/// <returns></returns> | |
public static string EnsurePostfix(char postfix, string value) | |
{ | |
if (string.IsNullOrEmpty(value)) | |
return postfix.ToString(); | |
if ((int) value[value.Length - 1] == (int) postfix) | |
return value; | |
return value + postfix.ToString(); | |
} | |
/// <summary>Ensures that a string has a certain prefix.</summary> | |
/// <param name="prefix">The prefix.</param> | |
/// <param name="value">The value.</param> | |
/// <returns></returns> | |
public static string EnsurePrefix(char prefix, string value) | |
{ | |
if (string.IsNullOrEmpty(value)) | |
return prefix.ToString(); | |
if ((int) value[0] == (int) prefix) | |
return value; | |
return prefix.ToString() + value; | |
} | |
/// <summary>Escapes the apostrophe symbol.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns>The escaped text.</returns> | |
public static string EscapeApostrophe(string text) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
return text.Replace("'", "'"); | |
} | |
/// <summary>Escapes the backslash.</summary> | |
/// <param name="test">The name.</param> | |
/// <returns></returns> | |
public static string EscapeBackslash(string test) | |
{ | |
Assert.ArgumentNotNull((object) test, "text"); | |
return test.Replace("\\", "\\\\"); | |
} | |
/// <summary>Escapes the javascript string.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns></returns> | |
public static string EscapeJavascriptString(string text) | |
{ | |
return StringUtil.EscapeJavascriptString(text, true); | |
} | |
/// <summary>Escapes the javascript string.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="surroundWithQuotes">Surround with quotes.</param> | |
/// <returns></returns> | |
public static string EscapeJavascriptString(string text, bool surroundWithQuotes) | |
{ | |
Error.AssertString(text, "text", true); | |
text = new StringBuilder(text).Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\n\r", "\\n\\r").Replace("\r\n", "\\r\\n").Replace("\n", "\\n").Replace("\r", "\\r").ToString(); | |
if (surroundWithQuotes) | |
text = "\"" + text + "\""; | |
return text; | |
} | |
/// <summary>Escapes the quote.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns></returns> | |
public static string EscapeQuote(string text) | |
{ | |
Error.AssertString(text, "text", true); | |
return StringUtil.EscapeQuote(text, """); | |
} | |
/// <summary>Escapes the quote.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="escapedSequence">The escaped sequence to replace the quote</param> | |
/// <returns></returns> | |
public static string EscapeQuote(string text, string escapedSequence) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertString(escapedSequence, "escapedSequence", true); | |
text = text.Replace("\"", escapedSequence); | |
return text; | |
} | |
/// <summary> | |
/// Extracts a parameter string from a query string formatted string. | |
/// </summary> | |
/// <param name="name">The name of the parameter to find.</param> | |
/// <param name="parameters">A query string formatted string, e.g. "a=1&b=2&myparam=3"</param> | |
/// <returns>The value of the named parameter.</returns> | |
/// <remarks>If the name was not found, blank is returned.</remarks> | |
/// <example> | |
/// <code> | |
/// string parameters = "print=1&target=/sitecore/content/home&test=false"; | |
/// string print = StringUtil.ExtractParameter("print", parameters); // "1" | |
/// string target = StringUtil.ExtractParameter("target", parameters); // "/sitecore/content/home" | |
/// string dummy = StringUtil.ExtractParameter("dummy", parameters); // "" | |
/// </code> | |
/// </example> | |
public static string ExtractParameter(string name, string parameters) | |
{ | |
Error.AssertString(name, "name", false); | |
Error.AssertString(parameters, "parameters", true); | |
string[] strArray = parameters.Split('&'); | |
string str1 = name.ToLowerInvariant() + "="; | |
int length = str1.Length; | |
foreach (string str2 in strArray) | |
{ | |
if (str2.Length > length && str2.StartsWith(str1, StringComparison.OrdinalIgnoreCase)) | |
return str2.Substring(length); | |
} | |
return string.Empty; | |
} | |
/// <summary>Gets the last part.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="delimiter">The delimiter.</param> | |
/// <param name="defaultValue">The default value.</param> | |
/// <returns></returns> | |
public static string GetLastPart(string text, char delimiter, string defaultValue) | |
{ | |
Error.AssertString(text, "text", true); | |
int num = text.LastIndexOf(delimiter); | |
if (num >= 0 && num < text.Length - 1) | |
return text.Substring(num + 1); | |
return defaultValue; | |
} | |
/// <summary>Gets the last postfix part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="delimiter">The character delimiting the postfix | |
/// from the rest of the string.</param> | |
/// <returns> | |
/// <para>All characters of the string starting from character | |
/// after the last occurrence of <paramref name="delimiter" />.</para> | |
/// <para>If <paramref name="delimiter" /> is not found in the string, | |
/// an empty string is returned.</para> | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string postfix = StringUtil.GetPostfix("sc:menu:new", ':'); | |
/// MainUtil.Out(postfix); | |
/// </code> | |
/// Outputs: <c>new</c> | |
/// <seealso cref="M:Sitecore.StringUtil.GetPostfix(System.String,System.Char)" /> | |
/// </example> | |
public static string GetLastPostfix(string text, char delimiter) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
return StringUtil.GetLastPostfix(text, delimiter, string.Empty); | |
} | |
/// <summary>Gets the postfix part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="delimiter">The character delimiting the postfix | |
/// from the rest of the string.</param> | |
/// <param name="defaultValue">Value to return if <paramref name="delimiter" /> | |
/// is not found in <paramref name="text" /></param> | |
/// <returns> | |
/// <para>All characters of the string starting from character | |
/// after the last occurrence of <paramref name="delimiter" />.</para> | |
/// <para>If <paramref name="delimiter" /> is not found in the string, | |
/// <paramref name="defaultValue" /> is returned.</para> | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string postfix = StringUtil.GetPostfix("sc:menu:new", ':'); | |
/// MainUtil.Out(postfix); | |
/// </code> | |
/// Outputs: <c>new</c> | |
/// <seealso cref="M:Sitecore.StringUtil.GetPostfix(System.String,System.Char,System.String)" /> | |
/// </example> | |
public static string GetLastPostfix(string text, char delimiter, string defaultValue) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
Assert.ArgumentNotNull((object) defaultValue, "defaultValue"); | |
if (text.Length == 0) | |
return string.Empty; | |
int num = text.LastIndexOf(delimiter); | |
if (num < 0) | |
return defaultValue; | |
return text.Substring(num + 1); | |
} | |
/// <summary>Gets the longest prefix.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="delimiter">The delimiter.</param> | |
/// <returns></returns> | |
public static string GetLongestPrefix(string text, char delimiter) | |
{ | |
Error.AssertString(text, "text", true); | |
return StringUtil.GetLongestPrefix(text, delimiter, string.Empty); | |
} | |
/// <summary>Gets the longest prefix.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="delimiter">The delimiter.</param> | |
/// <param name="defaultValue">The default value.</param> | |
/// <returns></returns> | |
public static string GetLongestPrefix(string text, char delimiter, string defaultValue) | |
{ | |
Error.AssertString(text, "text", true); | |
int length = text.LastIndexOf(delimiter); | |
if (length >= 0) | |
return text.Substring(0, length); | |
return defaultValue; | |
} | |
/// <summary>Converts a string to a name/value collection.</summary> | |
/// <param name="list">A string of separated values.</param> | |
/// <returns>A name/value collection.</returns> | |
/// <remarks>The format of the string must be "name=value|name=value...". | |
/// The name/value collection is built by using values from the string in pairs. | |
/// A string consisting of "Name1=Value1|Name2=Value2" are converted to | |
/// ("Name1", "Value1"), ("Name2", "Value2").</remarks> | |
/// <example> | |
/// <code> | |
/// NameValueCollection collection = StringUtil.GetNameValues("Name1=Value1|Name2=Value2"); | |
/// </code> | |
/// </example> | |
public static NameValueCollection GetNameValues(string list) | |
{ | |
return StringUtil.GetNameValues(list, '=', '|'); | |
} | |
/// <summary>Converts a string to a name/value collection.</summary> | |
/// <param name="list">A string of separated values.</param> | |
/// <param name="equal">Name/value pair separator.</param> | |
/// <param name="separator">Pair separator.</param> | |
/// <returns>A name/value collection.</returns> | |
/// <remarks>The name/value collection is built by using values from the string in pairs. | |
/// A string consisting of "Name1=Value1|Name2=Value2" are converted to | |
/// ("Name1", "Value1"), ("Name2", "Value2").</remarks> | |
/// <example> | |
/// <code> | |
/// NameValueCollection collection = StringUtil.GetNameValues("Name1=Value1|Name2=Value2", '=', '|'); | |
/// </code> | |
/// </example> | |
public static NameValueCollection GetNameValues(string list, char equal, char separator) | |
{ | |
return StringUtil.GetNameValues(list.Split(new char[2]{ equal, separator })); | |
} | |
/// <summary>Gets a name/value collection from a string array.</summary> | |
/// <param name="list">An array of strings.</param> | |
/// <returns>A name/value collection.</returns> | |
/// <remarks>The name/value collection is built by using values from the string array in pairs. | |
/// A string array consisting of { "Name1, "Value1", "Name2", "Value2" } are converted to | |
/// ("Name1", "Value1"), ("Name2", "Value2").</remarks> | |
/// <example> | |
/// <code> | |
/// NameValueCollection collection = StringUtil.GetNameValues(new string[] {"Name1", "Value1", "Name2", "Value2" }); | |
/// </code> | |
/// </example> | |
public static NameValueCollection GetNameValues(string[] list) | |
{ | |
NameValueCollection nameValueCollection = new NameValueCollection(); | |
int index = 0; | |
while (index < list.Length - 1) | |
{ | |
nameValueCollection.Add(list[index].Trim(), list[index + 1].Trim()); | |
index += 2; | |
} | |
return nameValueCollection; | |
} | |
/// <summary>Gets the postfix part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="delimiter">The character delimiting the postfix | |
/// from the rest of the string.</param> | |
/// <returns> | |
/// <para>All characters of the string starting from character | |
/// after the first occurrence of <paramref name="delimiter" />.</para> | |
/// <para>If <paramref name="delimiter" /> is not found in the string, | |
/// an empty string is returned.</para> | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string postfix = StringUtil.GetPostfix("sc:menu", ':'); | |
/// MainUtil.Out(postfix); | |
/// </code> | |
/// Outputs: <c>menu</c> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Mid(System.String,System.Int32)" /> | |
/// </example> | |
public static string GetPostfix(string text, char delimiter) | |
{ | |
Error.AssertString(text, "text", true); | |
return StringUtil.GetPostfix(text, delimiter, string.Empty); | |
} | |
/// <summary>Gets the postfix part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="delimiter">The character delimiting the postfix | |
/// from the rest of the string.</param> | |
/// <param name="defaultValue">Value to return if <paramref name="delimiter" /> | |
/// is not found in <paramref name="text" /></param> | |
/// <returns> | |
/// <para>All characters of the string starting from character | |
/// after the first occurrence of <paramref name="delimiter" />.</para> | |
/// <para>If <paramref name="delimiter" /> is not found in the string, | |
/// <paramref name="defaultValue" /> is returned.</para> | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string postfix = StringUtil.GetPostfix("sc:menu", ':'); | |
/// MainUtil.Out(postfix); | |
/// </code> | |
/// Outputs: <c>menu</c> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Mid(System.String,System.Int32)" /> | |
/// </example> | |
public static string GetPostfix(string text, char delimiter, string defaultValue) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
Assert.ArgumentNotNull((object) defaultValue, "defaultValue"); | |
int num = text.IndexOf(delimiter); | |
if (num >= 0) | |
return StringUtil.Mid(text, num + 1); | |
return defaultValue; | |
} | |
/// <summary>Gets the prefix part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="delimiter">The character delimiting the prefix | |
/// from the rest of the string.</param> | |
/// <returns> | |
/// <para>All characters of the string up to the first | |
/// occurrence of <paramref name="delimiter" />.</para> | |
/// <para>If <paramref name="delimiter" /> is not found in the string, | |
/// an empty string is returned.</para> | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string prefix = StringUtil.GetPrefix("sc:menu", ':'); | |
/// MainUtil.Out(prefix); | |
/// </code> | |
/// Outputs: <c>sc</c> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Mid(System.String,System.Int32)" /> | |
/// </example> | |
public static string GetPrefix(string text, char delimiter) | |
{ | |
Error.AssertString(text, "text", true); | |
return StringUtil.GetPrefix(text, delimiter, string.Empty); | |
} | |
/// <summary>Gets the prefix part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="delimiter">The character delimiting the prefix | |
/// from the rest of the string.</param> | |
/// <param name="defaultValue">Value to return if <paramref name="delimiter" /> | |
/// is not found in <paramref name="text" /></param> | |
/// <returns> | |
/// <para>All characters of the string up to the first | |
/// occurrence of <paramref name="delimiter" />.</para> | |
/// <para>If <paramref name="delimiter" /> is not found in the string, | |
/// <paramref name="defaultValue" /> is returned.</para> | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string prefix = StringUtil.GetPrefix("sc:menu", ':'); | |
/// MainUtil.Out(prefix); | |
/// </code> | |
/// Outputs: <c>sc</c> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Mid(System.String,System.Int32)" /> | |
/// </example> | |
public static string GetPrefix(string text, char delimiter, string defaultValue) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertString(defaultValue, "defaultValue", true); | |
int length = text.IndexOf(delimiter); | |
if (length > 0) | |
return text.Substring(0, length); | |
return defaultValue; | |
} | |
/// <summary>Gets the size string.</summary> | |
/// <param name="size">The size.</param> | |
/// <returns></returns> | |
[Obsolete("Please use 'MainUtil.FormatSize(size, translate: false)' API instead.")] | |
public static string GetSizeString(long size) | |
{ | |
return MainUtil.FormatSize(size, false); | |
} | |
/// <summary>Gets the string.</summary> | |
/// <param name="value">The value.</param> | |
/// <returns>The string.</returns> | |
/// <contract> | |
/// <requires name="value" condition="none" /> | |
/// <ensures condition="nullable" /> | |
/// </contract> | |
public static string GetString(object value) | |
{ | |
return StringUtil.GetString(value, string.Empty); | |
} | |
/// <summary>Gets the string.</summary> | |
/// <param name="value">The value.</param> | |
/// <param name="defaultValue">The default value.</param> | |
/// <returns>The string.</returns> | |
/// <contract> | |
/// <requires name="value" condition="none" /> | |
/// <requires name="defaultValue" condition="none" /> | |
/// <ensures condition="nullable" /> | |
/// </contract> | |
public static string GetString(object value, string defaultValue) | |
{ | |
if (value == null) | |
return defaultValue; | |
string str = value.ToString(); | |
if (str.Length > 0) | |
return str; | |
return defaultValue; | |
} | |
/// <summary>Gets a non-empty string from a list.</summary> | |
/// <param name="values">The values.</param> | |
/// <returns>The string.</returns> | |
/// <contract> | |
/// <requires name="values" condition="not null" /> | |
/// <ensures condition="not null" /> | |
/// </contract> | |
public static string GetString(params string[] values) | |
{ | |
Assert.ArgumentNotNull((object) values, "values"); | |
foreach (string str in values) | |
{ | |
if (!string.IsNullOrEmpty(str)) | |
return str; | |
} | |
return string.Empty; | |
} | |
/// <summary> | |
/// Searches for the text and returns the zero-based index of the first occurrence within the entire string. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
/// <param name="texts">The texts.</param> | |
/// <returns></returns> | |
public static int IndexOf(string text, string[] texts) | |
{ | |
if (text != null && texts != null && texts.Length > 0) | |
{ | |
for (int index = 0; index < texts.Length; ++index) | |
{ | |
if (texts[index] == text) | |
return index; | |
} | |
} | |
return -1; | |
} | |
/// <summary> | |
/// Searches for the text and returns the zero-based index of the first occurrence within the entire string. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
/// <param name="value">The value.</param> | |
/// <param name="occurrence">The occurrence.</param> | |
/// <returns></returns> | |
public static int IndexOf(string text, char value, int occurrence) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertInt(occurrence, "occurrence", false, false); | |
int num = 0; | |
for (int index = 0; index < text.Length; ++index) | |
{ | |
if ((int) text[index] == (int) value) | |
{ | |
++num; | |
if (num == occurrence) | |
return index; | |
} | |
} | |
return -1; | |
} | |
/// <summary> | |
/// Determines whether [is white space] [the specified s]. | |
/// </summary> | |
/// <param name="s">The s.</param> | |
/// <returns> | |
/// <c>true</c> if [is white space] [the specified s]; otherwise, <c>false</c>. | |
/// </returns> | |
public static bool IsWhiteSpace(string s) | |
{ | |
return Regex.IsMatch(s, "^[\\s]*$"); | |
} | |
/// <summary>Joins the specified enumerable.</summary> | |
/// <param name="enumerable">The enumerable.</param> | |
/// <param name="separator">The separator.</param> | |
/// <returns>The join.</returns> | |
public static string Join(IEnumerable enumerable, string separator) | |
{ | |
Assert.ArgumentNotNull((object) enumerable, "enumerable"); | |
Assert.ArgumentNotNull((object) separator, "separator"); | |
StringBuilder stringBuilder = new StringBuilder(); | |
bool flag = true; | |
foreach (object obj in enumerable) | |
{ | |
if (obj != null) | |
{ | |
if (!flag) | |
stringBuilder.Append(separator); | |
flag = false; | |
stringBuilder.Append(obj.ToString()); | |
} | |
} | |
return stringBuilder.ToString(); | |
} | |
/// <summary>Joins the specified enumerable.</summary> | |
/// <param name="enumerable">The enumerable.</param> | |
/// <param name="separator">The separator.</param> | |
/// <param name="propertyName">Name of the property.</param> | |
/// <returns>The join.</returns> | |
public static string Join(IEnumerable enumerable, string separator, string propertyName) | |
{ | |
Assert.ArgumentNotNull((object) enumerable, "enumerable"); | |
Assert.ArgumentNotNull((object) separator, "separator"); | |
Assert.ArgumentNotNullOrEmpty(propertyName, "propertyName"); | |
StringBuilder stringBuilder = new StringBuilder(); | |
bool flag = true; | |
foreach (object obj in enumerable) | |
{ | |
if (obj != null) | |
{ | |
if (!flag) | |
stringBuilder.Append(separator); | |
flag = false; | |
stringBuilder.Append(ReflectionUtil.GetProperty(obj, propertyName).ToString()); | |
} | |
} | |
return stringBuilder.ToString(); | |
} | |
/// <summary>Gets the leftmost part of a string.</summary> | |
/// <param name="text">A string</param> | |
/// <param name="length">The number of character to return.</param> | |
/// <returns> | |
/// The first <i>length</i> characters of the string. | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// string left = StringUtil.Left("Hello world", 5); // "Hello" | |
/// </code> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Mid(System.String,System.Int32)" /> | |
/// </example> | |
public static string Left(string text, int length) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
if (length <= 0) | |
return string.Empty; | |
if (text.Length <= length) | |
return text; | |
return text.Substring(0, length); | |
} | |
/// <summary>Gets the middle part of a string.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="start">The starting index into the string.</param> | |
/// <returns> | |
/// The rightmost part of the string starting at the <i>start</i> position. | |
/// </returns> | |
/// <remarks>If <i>start</i> is greater than the length of the string, the blank is returned.</remarks> | |
/// <example> | |
/// <code> | |
/// string result = StringUtil.Mid("Hello world", 6); / "world" | |
/// </code> | |
/// <seealso cref="M:Sitecore.StringUtil.Left(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// </example> | |
public static string Mid(string text, int start) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
if (start >= text.Length || start < 0) | |
return string.Empty; | |
return text.Substring(start); | |
} | |
/// <summary>Gets the middle part of a string.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="start">The starting index into the string.</param> | |
/// <param name="length">The number of returned characters</param> | |
/// <returns> | |
/// The rightmost part of the string starting at the <i>start</i> position. | |
/// </returns> | |
/// <remarks></remarks> | |
/// <example> | |
/// <code> | |
/// string result = StringUtil.Mid("Hello world", 6, 3); / "wor" | |
/// </code> | |
/// <seealso cref="M:Sitecore.StringUtil.Left(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Right(System.String,System.Int32)" /> | |
/// </example> | |
public static string Mid(string text, int start, int length) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
if (length <= 0 || start >= text.Length || start < 0) | |
return string.Empty; | |
int num = text.Length - start; | |
if (length >= num) | |
return text.Substring(start); | |
return text.Substring(start, length); | |
} | |
/// <summary>Names the values to string.</summary> | |
/// <param name="values">The values.</param> | |
/// <returns></returns> | |
public static string NameValuesToString(NameValueCollection values) | |
{ | |
if (values != null && values.Count > 0) | |
return StringUtil.NameValuesToString(values, ", "); | |
return string.Empty; | |
} | |
/// <summary>Names the values to string.</summary> | |
/// <param name="values">The values.</param> | |
/// <param name="divider">The divider.</param> | |
/// <returns></returns> | |
public static string NameValuesToString(NameValueCollection values, string divider) | |
{ | |
if (values == null || values.Count <= 0) | |
return string.Empty; | |
Error.AssertString(divider, "divider", true); | |
string str = string.Empty; | |
foreach (string key in values.Keys) | |
str = str + key + "=" + values[key] + divider; | |
if (str.Length > 0) | |
str = StringUtil.TrimEnd(str, divider.Length); | |
return str; | |
} | |
/// <summary>Names the values to dictionary.</summary> | |
/// <param name="values">The values.</param> | |
/// <returns>The values to dictionary.</returns> | |
public static SafeDictionary<string> NameValuesToDictionary(NameValueCollection values) | |
{ | |
Assert.ArgumentNotNull((object) values, "values"); | |
SafeDictionary<string> safeDictionary = new SafeDictionary<string>(); | |
foreach (string key in values.Keys) | |
safeDictionary.Add(key, values[key]); | |
return safeDictionary; | |
} | |
/// <summary>Parses an argument list into a string array.</summary> | |
/// <param name="arguments">The input string.</param> | |
/// <returns></returns> | |
/// <remarks> | |
/// The input string has the format: '"String1", "String2"'. This will result | |
/// in an array with two elements: String1 and String2. To escape the double | |
/// quote character (") use two consequetive double quotes (""). | |
/// </remarks> | |
public static string[] ParseArguments(string arguments) | |
{ | |
MatchCollection matchCollection = Regex.Matches(arguments, "(\"([^\"]|\"\")*\")"); | |
string[] strArray = new string[matchCollection.Count]; | |
for (int index = 0; index < matchCollection.Count; ++index) | |
strArray[index] = StringUtil.Unquote(matchCollection[index].Value); | |
return strArray; | |
} | |
/// <summary>Parses the name value.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="nameSeparator">The name separator.</param> | |
/// <param name="valueSeparator">The value separator.</param> | |
/// <returns>The name value.</returns> | |
public static NameValueCollection ParseNameValueCollection(string text, char nameSeparator, char valueSeparator) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
NameValueCollection nameValueCollection = new NameValueCollection(); | |
if (string.IsNullOrEmpty(text)) | |
return nameValueCollection; | |
string str1 = text; | |
char[] chArray = new char[1]{ nameSeparator }; | |
foreach (string str2 in str1.Split(chArray)) | |
{ | |
string index = str2.Trim(); | |
if (!string.IsNullOrEmpty(index)) | |
{ | |
string str3 = string.Empty; | |
int length = index.IndexOf(valueSeparator); | |
if (length >= 0) | |
{ | |
str3 = index.Substring(length + 1).Trim(); | |
index = index.Substring(0, length).Trim(); | |
} | |
nameValueCollection[index] = str3; | |
} | |
} | |
return nameValueCollection; | |
} | |
/// <summary>Parses a size string (10KB, 20MB, etc.).</summary> | |
/// <param name="value">The value.</param> | |
/// <returns></returns> | |
public static long ParseSizeString(string value) | |
{ | |
if (value != null && value.Length > 0) | |
{ | |
value = value.Replace(".", string.Empty); | |
value = value.Replace(" ", string.Empty); | |
int length = value.Length; | |
if (length > 0) | |
{ | |
int num1 = 1; | |
if ((int) value[length - 1] == 66) | |
{ | |
switch (value[length - 2]) | |
{ | |
case 'G': | |
num1 = 1073741824; | |
break; | |
case 'K': | |
num1 = 1024; | |
break; | |
case 'M': | |
num1 = 1048576; | |
break; | |
} | |
value = value.Substring(0, length - 2); | |
} | |
long num2 = MainUtil.GetLong((object) value, -1L); | |
if (num2 >= 0L) | |
return num2 * (long) num1; | |
} | |
} | |
return -1; | |
} | |
/// <summary>Reads a phrase from a string.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="start">Starting position into the string.</param> | |
/// <param name="delimiter">A delimiter character.</param> | |
/// <param name="quote">A quote character.</param> | |
/// <param name="endPos">Returns the ending position.</param> | |
/// <returns>The phrase.</returns> | |
/// <remarks>A phrase is single word or words within quotes.</remarks> | |
public static string ReadPhrase(string text, int start, char delimiter, char quote, out int endPos) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertInt(start, "start", true, false); | |
endPos = -1; | |
if (start < text.Length) | |
{ | |
while (start < text.Length && (int) text[start] == (int) delimiter) | |
++start; | |
if (start < text.Length) | |
{ | |
char ch = delimiter; | |
if ((int) text[start] == (int) quote) | |
{ | |
ch = quote; | |
++start; | |
} | |
int num = -1; | |
for (int index = start; index < text.Length; ++index) | |
{ | |
if ((int) text[index] == (int) ch) | |
{ | |
num = index; | |
break; | |
} | |
} | |
if (num == -1) | |
num = text.Length; | |
if (num <= start) | |
return StringUtil.ReadPhrase(text, num + 1, delimiter, quote, out endPos); | |
endPos = (int) ch != (int) quote ? num - 1 : num; | |
return text.Substring(start, num - start); | |
} | |
} | |
return string.Empty; | |
} | |
/// <summary>Remove tags from a string.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns></returns> | |
public static string RemoveLineFeeds(string text) | |
{ | |
Error.AssertString(text, "text", true); | |
return text.Replace("\r", string.Empty).Replace("\n", string.Empty); | |
} | |
/// <summary>Removes a postfix from a string.</summary> | |
/// <param name="postfix">The postfix.</param> | |
/// <param name="value">The string value.</param> | |
/// <returns></returns> | |
public static string RemovePostfix(char postfix, string value) | |
{ | |
Assert.ArgumentNotNull((object) value, "value"); | |
if (value.Length == 0) | |
return string.Empty; | |
if ((int) value[value.Length - 1] != (int) postfix) | |
return value; | |
return value.Substring(0, value.Length - 1); | |
} | |
/// <summary>Removes a specific prefix from a string.</summary> | |
/// <param name="postfix">The postfix.</param> | |
/// <param name="value">The value.</param> | |
/// <returns></returns> | |
public static string RemovePostfix(string postfix, string value) | |
{ | |
Assert.ArgumentNotNullOrEmpty(postfix, "postfix"); | |
Assert.ArgumentNotNull((object) value, "value"); | |
if (postfix.Length == 0 || !value.EndsWith(postfix, StringComparison.OrdinalIgnoreCase)) | |
return value; | |
return value.Substring(0, value.Length - postfix.Length); | |
} | |
/// <summary>Removes a specific prefix from a string.</summary> | |
/// <param name="prefix">The prefix.</param> | |
/// <param name="value">The value.</param> | |
public static string RemovePrefix(string prefix, string value) | |
{ | |
Assert.ArgumentNotNull((object) prefix, "prefix"); | |
Assert.ArgumentNotNull((object) value, "value"); | |
if (prefix.Length == 0 || !value.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) | |
return value; | |
return value.Substring(prefix.Length); | |
} | |
/// <summary>Removes a prefix from a string.</summary> | |
/// <param name="prefix">The prefix.</param> | |
/// <param name="value">The string value.</param> | |
/// <returns></returns> | |
public static string RemovePrefix(char prefix, string value) | |
{ | |
if (string.IsNullOrEmpty(value) || (int) value[0] != (int) prefix) | |
return value; | |
return StringUtil.Mid(value, 1); | |
} | |
/// <summary>Remove tags from a string.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns></returns> | |
public static string RemoveTags(string text) | |
{ | |
Error.AssertString(text, "text", true); | |
return StringUtil.m_removeTagsRegex.Replace(text, string.Empty); | |
} | |
/// <summary>Repeats a string a number of times.</summary> | |
/// <param name="s">The string to be repeated.</param> | |
/// <param name="nCount">The number of times the string should be repeated.</param> | |
/// <returns>The string repeated the number of times.</returns> | |
/// <example> | |
/// <code> | |
/// string text = MainUtil.Repeat("abc", 3); // "abcabcabc" | |
/// </code> | |
/// </example> | |
public static string Repeat(string s, int nCount) | |
{ | |
StringBuilder stringBuilder = new StringBuilder(nCount * s.Length); | |
for (int index = 0; index < nCount; ++index) | |
stringBuilder.Append(s); | |
return stringBuilder.ToString(); | |
} | |
/// <summary>Replace a character array with strings.</summary> | |
/// <param name="text">The text.</param> | |
/// <param name="find">The find.</param> | |
/// <param name="replaceWith">The replace.</param> | |
/// <returns></returns> | |
public static string Replace(string text, char[] find, string[] replaceWith) | |
{ | |
Error.AssertObject((object) find, "find"); | |
Error.AssertObject((object) replaceWith, "replace"); | |
if (text.Length > 0 && find.Length == replaceWith.Length) | |
{ | |
for (int index = 0; index < find.Length; ++index) | |
{ | |
if (text.IndexOf(find[index]) >= 0) | |
text = text.Replace(find[index].ToString(), replaceWith[index]); | |
} | |
} | |
return text; | |
} | |
/// <summary>Gets the rightmost part of a string.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="length">The number of characters to return.</param> | |
/// <returns>The rightmost <i>length</i> characters.</returns> | |
/// <example> | |
/// <code> | |
/// string text = StringUtil.Right("Hello world", 5); // "world" | |
/// </code> | |
/// <seealso cref="M:Sitecore.StringUtil.Left(System.String,System.Int32)" /> | |
/// <seealso cref="M:Sitecore.StringUtil.Mid(System.String,System.Int32)" /> | |
/// </example> | |
public static string Right(string text, int length) | |
{ | |
Error.AssertString(text, "text", true); | |
Error.AssertInt(length, "length", true, false); | |
if (length >= text.Length) | |
return text; | |
return text.Substring(text.Length - length); | |
} | |
/// <summary>Splits a string while protecting phrases.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="delimiter">A delimiter character</param> | |
/// <param name="quote">A quoute character.</param> | |
/// <returns>An array list of string parts.</returns> | |
/// <remarks>A phrase is a word within quotes.</remarks> | |
/// <example> | |
/// <code> | |
/// ArrayList list = StringUtil.Split("Welcome 'Andy Parkinson' to Sitecore", ' ', ''''); | |
/// // "Welcome", "Andy Parkinson", "to", "Sitecore" | |
/// </code> | |
/// </example> | |
public static ArrayList Split(string text, char delimiter, char quote) | |
{ | |
Error.AssertString(text, "text", true); | |
ArrayList arrayList = new ArrayList(); | |
int endPos; | |
for (string str = StringUtil.ReadPhrase(text, 0, delimiter, quote, out endPos); !string.IsNullOrEmpty(str); str = StringUtil.ReadPhrase(text, endPos + 1, delimiter, quote, out endPos)) | |
{ | |
if ((int) str[0] == (int) quote) | |
str = str.Substring(1, str.Length - 2); | |
if (str.Length > 0) | |
arrayList.Add((object) str); | |
} | |
return arrayList; | |
} | |
/// <summary>Splits a string, optionally trimming parts.</summary> | |
/// <param name="text">A string.</param> | |
/// <param name="delimiter">A delimiter character.</param> | |
/// <param name="trim">A boolean indicating if the parts should be trimmed.</param> | |
/// <returns>An array list of string parts.</returns> | |
/// <example> | |
/// <code> | |
/// ArrayList list1 = StringUtil.Split("Hello | world", '|', true); /// "Hello", "world" | |
/// ArrayList list2 = StringUtil.Split("Hello | world", '|', false); /// "Hello ", " world" | |
/// </code> | |
/// </example> | |
public static string[] Split(string text, char delimiter, bool trim) | |
{ | |
Error.AssertString(text, "text", true); | |
string[] strArray = text.Split(delimiter); | |
if (trim) | |
{ | |
for (int index = 0; index < strArray.Length; ++index) | |
strArray[index] = strArray[index].Trim(); | |
} | |
return strArray; | |
} | |
/// <summary>Strings the array to name value collection.</summary> | |
/// <param name="strings">The strings.</param> | |
/// <returns></returns> | |
public static NameValueCollection StringArrayToNameValueCollection(string[] strings) | |
{ | |
Error.AssertObject((object) strings, "strings"); | |
Error.Assert(strings.Length % 2 == 0, "String array must contain an even number of elements."); | |
NameValueCollection nameValueCollection = new NameValueCollection(); | |
int index = 0; | |
while (index < strings.Length - 1) | |
{ | |
nameValueCollection.Add(strings[index], strings[index + 1]); | |
index += 2; | |
} | |
return nameValueCollection; | |
} | |
/// <summary>Converts a string collection to a string</summary> | |
/// <param name="collection">A collection of strings.</param> | |
/// <returns> | |
/// The strings in collection concatenated with a pipe (|) in between. | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// StringCollection list = new StringCollection(); | |
/// list.Add("1"); | |
/// list.Add("2"); | |
/// list.Add("3"); | |
/// string result = StringUtil.StringCollectionToString(list); / "1|2|3" | |
/// </code> | |
/// </example> | |
public static string StringCollectionToString(StringCollection collection) | |
{ | |
return StringUtil.StringCollectionToString(collection, "|"); | |
} | |
/// <summary>Converts a string collection to a string</summary> | |
/// <param name="collection">A collection of strings.</param> | |
/// <param name="delimiter">A string to put between each of the strings in the collection.</param> | |
/// <returns> | |
/// The strings in collection concatenated with the delimiter in between. | |
/// </returns> | |
/// <example> | |
/// <code> | |
/// StringCollection list = new StringCollection(); | |
/// list.Add("1"); | |
/// list.Add("2"); | |
/// list.Add("3"); | |
/// string result = StringUtil.StringCollectionToString(list, "|"); / "1|2|3" | |
/// </code> | |
/// </example> | |
public static string StringCollectionToString(StringCollection collection, string delimiter) | |
{ | |
Error.AssertObject((object) collection, "collection"); | |
StringBuilder stringBuilder = new StringBuilder(); | |
lock (collection.SyncRoot) | |
{ | |
for (int index = 0; index < collection.Count; ++index) | |
{ | |
if (index > 0) | |
stringBuilder.Append(delimiter); | |
stringBuilder.Append(collection[index]); | |
} | |
} | |
return stringBuilder.ToString(); | |
} | |
/// <summary>Trims the end.</summary> | |
/// <param name="value">The value.</param> | |
/// <param name="count">The count.</param> | |
/// <returns></returns> | |
public static string TrimEnd(string value, int count) | |
{ | |
int length = value.Length - count; | |
if (length <= 0) | |
return string.Empty; | |
return value.Substring(0, length); | |
} | |
/// <summary>Trims the start.</summary> | |
/// <param name="value">The value.</param> | |
/// <param name="count">The count.</param> | |
/// <returns></returns> | |
public static string TrimStart(string value, int count) | |
{ | |
if (value.Length - count <= 0) | |
return string.Empty; | |
return value.Substring(count); | |
} | |
/// <summary>Unescapes the apostrophe symbol.</summary> | |
/// <param name="text">The text.</param> | |
/// <returns>The apostrophe.</returns> | |
public static string UnescapeApostrophe(string text) | |
{ | |
Assert.ArgumentNotNull((object) text, "text"); | |
return text.Replace("'", "'"); | |
} | |
/// <summary> | |
/// Removes enclosing single and double quotes from a string. | |
/// </summary> | |
/// <param name="text">A string.</param> | |
/// <returns>The unquoted string.</returns> | |
/// <example> | |
/// <code> | |
/// string s0 = StringUtil.Unquote("'Hello world'"); // "Hello world" | |
/// string s1 = StringUtil.Unquote("'Hello Andy's world'"); // "Hello Andy's world" | |
/// </code> | |
/// </example> | |
public static string Unquote(string text) | |
{ | |
text = text.Trim(); | |
if (text == "\"" || text == "'") | |
text = string.Empty; | |
else if (text.StartsWith("\"", StringComparison.InvariantCulture) && text.EndsWith("\"", StringComparison.InvariantCulture)) | |
text = text.Substring(1, text.Length - 2); | |
else if (text.StartsWith("'", StringComparison.InvariantCulture) && text.EndsWith("'", StringComparison.InvariantCulture)) | |
text = text.Substring(1, text.Length - 2); | |
return text; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment