123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace EasyDevCore.Common
- {
- /// <summary>
- /// String extension helper
- /// </summary>
- public static class StringExtensions
- {
- /// <summary>
- /// Ins the specified string comparison.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="comparisionType">One of the enumeration values that specifies how the strings will be compared.</param>
- /// <param name="args">The arguments.</param>
- /// <returns></returns>
- public static bool In(this string input, StringComparison comparisionType, params string[] args)
- {
- if (input == null) return false;
- foreach (string value in args)
- {
- if (!input.Equals(value))
- continue;
- return true;
- }
- return false;
- }
- /// <summary>
- /// Determines whether the specified arguments has any.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="args">The arguments.</param>
- /// <returns>
- /// <c>true</c> if the specified arguments has any; otherwise, <c>false</c>.
- /// </returns>
- public static bool HasAny(this string input, params string[] args)
- {
- foreach(var p in args)
- {
- if (input.IndexOf(p) > -1) return true;
- }
- return false;
- }
- /// <summary>
- /// Determines whether the specified comparison has any.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="comparison">The comparison.</param>
- /// <param name="args">The arguments.</param>
- /// <returns>
- /// <c>true</c> if the specified comparison has any; otherwise, <c>false</c>.
- /// </returns>
- public static bool HasAny(this string input, StringComparison comparison, params string[] args)
- {
- foreach (var p in args)
- {
- if (input.IndexOf(p, comparison) > -1) return true;
- }
- return false;
- }
- /// <summary>
- /// Determines whether [is unicode string] [the specified input].
- /// </summary>
- /// <param name="input">The input.</param>
- /// <returns>
- /// <c>true</c> if [is unicode string] [the specified input]; otherwise, <c>false</c>.
- /// </returns>
- public static bool IsUnicode(this string input)
- {
- return input.ToCharArray().Any(c => c > 255);
- }
- /// <summary>
- /// Removes all leading occurrences of whitespace of input string
- /// </summary>
- /// <param name="input">The input.</param>
- /// <returns></returns>
- public static string LTrim(this string input)
- {
- return input.TrimStart(new char[] { ' ' });
- }
- /// <summary>
- /// Removes all occurrences of whitespace of the end of the input string
- /// </summary>
- /// <param name="input">The input.</param>
- /// <returns></returns>
- public static string RTrim(this string input)
- {
- return input.TrimEnd(new char[] { ' ' });
- }
- /// <summary>
- /// Lefts the specified length.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="len">The length.</param>
- /// <returns></returns>
- public static string Left(this string input, int len) => (input == null || input.Length <= len) ? input : input.Substring(0, len);
- /// <summary>
- /// Rights the specified length.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="len">The length.</param>
- /// <returns></returns>
- public static string Right(this string input, int len) => (input == null || input.Length <= len) ? input : input.Substring(len - 1, input.Length - len);
- /// <summary>
- /// Replaces with case style.
- /// </summary>
- /// <param name="text">The text.</param>
- /// <param name="oldValue">The old value.</param>
- /// <param name="newValue">The new value.</param>
- /// <param name="maxMatchLength">Maximum length of the match.</param>
- /// <returns></returns>
- public static string ReplaceCaseStyle(this string text, string oldValue, string newValue, int maxMatchLength = 0)
- {
- var caseReplacement = TransferCaseStyle(oldValue, newValue);
- return text.Replace(oldValue, caseReplacement);
- }
- /// <summary>
- /// Transfers the case style.
- /// </summary>
- /// <param name="match">The match.</param>
- /// <param name="replacement">The replacement.</param>
- /// <param name="maxMatchLength">Maximum length of the match.</param>
- /// <returns></returns>
- public static string TransferCaseStyle(string match, string replacement, int maxMatchLength = 0)
- {
- char[] replacementChars = replacement.ToCharArray();
- if(maxMatchLength == 0)
- {
- maxMatchLength = Math.Min(match.Length, replacement.Length);
- }
- for (int i = 0; i < maxMatchLength; i++)
- {
- if (char.IsLower(match[i]))
- replacementChars[i] = char.ToLower(replacementChars[i]);
- else if (char.IsUpper(match[i]))
- replacementChars[i] = char.ToUpper(replacementChars[i]);
- }
- return new string(replacementChars);
- }
- /// <summary>
- /// Regexes the get match values.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="patterns">The patterns.</param>
- /// <returns>(Key = Match Value, Value = Group Name)</returns>
- public static Dictionary<string, string> RegexGetMatchValues(this string input, string patterns)
- {
- Dictionary<string, string> matchValues = new();
- Regex re = new Regex(patterns);
- var groupNames = re.GetGroupNames().Where(x => !(x[0] >= (byte)'0' && x[0] <= (byte)'9')).ToArray();
- var matches = re.Matches(input);
- foreach (Match m in matches.AsParallel())
- {
- foreach (var gn in groupNames)
- {
- var mg = m.Groups[gn];
- if (mg.Success)
- {
- var value = mg.Value;
- if (!matchValues.ContainsKey(value))
- {
- matchValues.Add(value, gn);
- }
- }
- }
- }
- return matchValues;
- }
- /// <summary>
- /// Regexes the group match.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="patterns">The patterns.</param>
- /// <param name="matchHandler">The match handler is Func(Group match, string groupname, string replactedText).</param>
- public static void RegexGroupMatch(this string input, string patterns, Action<Group> matchHandler)
- {
- Regex re = new Regex(patterns);
- var groupNames = re.GetGroupNames().Where(x => !(x[0] >= (byte)'0' && x[0] <= (byte)'9')).ToArray();
- var matches = re.Matches(input);
- foreach (Match m in matches.AsParallel())
- {
- foreach (var gn in groupNames)
- {
- var mg = m.Groups[gn];
- if (mg.Success)
- {
- matchHandler(mg);
- }
- }
- }
- }
- /// <summary>
- /// Regexes the group replace.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="patterns">The patterns.</param>
- /// <param name="matchHandler">The match handler is Func(Group match, string groupname, string replactedText).</param>
- /// <returns></returns>
- public static string RegexGroupReplace(this string input, string patterns, Func<Group, string, string> matchHandler)
- {
- Regex re = new Regex(patterns);
- var groupNames = re.GetGroupNames().Where(x => !(x[0] >= (byte)'0' && x[0] <= (byte)'9')).ToArray();
- return re.Replace(input, (MatchEvaluator)((m) =>
- {
- foreach (var gn in groupNames)
- {
- var mg = m.Groups[gn];
- if (mg.Success)
- {
- return matchHandler(mg, gn);
- }
- }
- return m.Value;
- }));
- }
- /// <summary>
- /// Substitudes the specified input.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="index">The index.</param>
- /// <param name="length">The length.</param>
- /// <param name="replacedText">The replaced text.</param>
- /// <returns></returns>
- public static string Substitude(this string input, int index, int length, string replacedText)
- {
- return input.Substring(0, index) + replacedText + (index + length > input.Length ? "" : input.Substring(index + length));
- }
- /// <summary>
- /// Substitudes the specified input.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="index">The index.</param>
- /// <param name="replacedText">The replaced text.</param>
- /// <returns></returns>
- public static string Substitude(this string input, int index, string replacedText)
- {
- return input.Substring(0, index) + replacedText + (index + replacedText.Length > input.Length ? "" : input.Substring(index + replacedText.Length));
- }
- /// <summary>
- /// Splits the string.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="seperator">The seperator.</param>
- /// <param name="splitOptions">The split options.</param>
- /// <param name="trimSpace">if set to <c>true</c> [trim space].</param>
- /// <returns></returns>
- public static string[] SplitString(this string input, string seperator, StringSplitOptions splitOptions = StringSplitOptions.RemoveEmptyEntries, bool trimSpace = false)
- {
- string[] result = input.Split(new string[] { seperator }, splitOptions);
- if (trimSpace)
- {
- return result.Select(s => s.Trim()).ToArray();
- }
- return result;
- }
- /// <summary>
- /// Splits the by comma or semi colon (,;).
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="splitOptions">The split options.</param>
- /// <param name="trimSpace">if set to <c>true</c> [trim space].</param>
- /// <returns>System.String[].</returns>
- public static string[] SplitByCommaOrSemiColon(this string input, StringSplitOptions splitOptions = StringSplitOptions.RemoveEmptyEntries, bool trimSpace = false)
- {
- string[] result = input.Split( new char[] { ',', ';' }, splitOptions);
- if (trimSpace)
- {
- return result.Select(s => s.Trim()).ToArray();
- }
- return result;
- }
- /// <summary>
- /// Splits the comma string.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="splitOptions">The split options.</param>
- /// <param name="trimSpace">if set to <c>true</c> [trim space].</param>
- /// <returns></returns>
- public static string[] SplitCommaString(this string input, StringSplitOptions splitOptions = StringSplitOptions.RemoveEmptyEntries, bool trimSpace = false)
- {
- string[] result = input.Split(new char[] { ',' }, splitOptions);
- if (trimSpace)
- {
- return result.Select(s => s.Trim()).ToArray();
- }
- return result;
- }
- /// <summary>
- /// Splits the semi colon string.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="splitOptions">The split options.</param>
- /// <param name="trimSpace">if set to <c>true</c> [trim space].</param>
- /// <returns></returns>
- public static string[] SplitSemiColonString(this string input, StringSplitOptions splitOptions = StringSplitOptions.RemoveEmptyEntries, bool trimSpace = false)
- {
- string[] result = input.Split(new char[] { ';' }, splitOptions);
- if (trimSpace)
- {
- return result.Select(s => s.Trim()).ToArray();
- }
- return result;
- }
- /// <summary>
- /// A case insenstive replace function.
- /// </summary>
- /// <param name="value">The string to examine.</param>
- /// <param name="newValue">The value to replace.</param>
- /// <param name="oldValue">The new value to be inserted</param>
- /// <returns>A string</returns>
- public static string CaseInsenstiveReplace(this string value, string newValue, string oldValue)
- {
- Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Multiline);
- return regEx.Replace(value, newValue);
- }
- /// <summary>
- /// Removes the new line (\n) and carriage return (\r) symbols.
- /// </summary>
- /// <param name="value">The string to search.</param>
- /// <param name="addSpace">If true, adds a space (" ") for each newline and carriage
- /// return found.</param>
- /// <returns>A string</returns>
- public static string RemoveNewLines(this string value, bool addSpace = false)
- {
- string replace = string.Empty;
- if (addSpace)
- replace = " ";
- string pattern = @"[\r|\n]";
- Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
- return regEx.Replace(value, replace);
- }
- /// <summary>
- /// Reverse a string.
- /// </summary>
- /// <param name="value">The string to reverse</param>
- /// <returns>A string</returns>
- public static string Reverse(this string value)
- {
- if (value.Length <= 1)
- return value;
- char[] c = value.ToCharArray();
- StringBuilder sb = new StringBuilder(c.Length);
- for (int i = c.Length - 1; i > -1; i--)
- sb.Append(c[i]);
- return sb.ToString();
- }
- /// <summary>
- /// Converts a string to sentence case.
- /// </summary>
- /// <param name="value">The string to convert.</param>
- /// <returns>A string</returns>
- public static string SentenceCase(this string value)
- {
- if (value.Length < 1)
- return value;
- // start by converting entire string to lower case
- var lowerCase = value.ToLower();
- // matches the first sentence of a string, as well as subsequent sentences
- var r = new Regex(@"(^[a-z])|\.\s+(.)", RegexOptions.ExplicitCapture);
- // MatchEvaluator delegate defines replacement of setence starts to uppercase
- return r.Replace(lowerCase, s => s.Value.ToUpper());
- }
-
- /// <summary>
- /// Converts a string to title case.
- /// </summary>
- /// <param name="value">The string to convert.</param>
- /// <returns>A string.</returns>
- public static string TitleCase(this string value)
- {
- if (value.Length < 1)
- return value;
- // start by converting entire string to lower case
- var lowerCase = value.ToLower();
- // matches the first sentence of a string, as well as subsequent sentences
- var r = new Regex(@"\b(\w)", RegexOptions.ExplicitCapture);
- // MatchEvaluator delegate defines replacement of setence starts to uppercase
- return r.Replace(lowerCase, s => s.Value.ToUpper());
- }
- /// <summary>
- /// Determines whether the specified input is match.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="pattern">The pattern (regular expression).</param>
- /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
- /// <returns>
- /// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
- /// </returns>
- public static bool IsMatch(this string input, string pattern, bool ignoreCase = false)
- {
- return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
- }
-
- /// <summary>
- /// string like operator.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="pattern">The pattern.</param>
- /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
- /// <returns></returns>
- public static bool StringLike(this string input, string pattern, bool ignoreCase = false)
- {
- return SQLLike(input, pattern.Replace("?", "_").Replace("*", "%"), ignoreCase);
- }
- /// <summary>
- /// SQL like operator.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="pattern">The pattern.</param>
- /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
- /// <returns></returns>
- public static bool SQLLike(this string input, string pattern, bool ignoreCase)
- {
- /* Turn "off" all regular expression related syntax in
- * the pattern string. */
- pattern = Regex.Escape(pattern);
- /* Replace the LIKE wildcard metacharacters with the
- * equivalent regular expression metacharacters. */
- pattern = pattern.Replace("_", ".").Replace("%", ".*");
- /* The previous call to Regex.Escape actually turned off
- * too many metacharacters, i.e. those which are recognized by
- * both the regular expression engine and the LIKE
- * statement ([...] and [^...]). Those metacharacters have
- * to be manually unescaped here. */
- pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
- if (ignoreCase)
- {
- return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase);
- }
- else
- {
- return Regex.IsMatch(input, pattern);
- }
- }
- /// <summary>
- /// SQLs the like pattern.
- /// </summary>
- /// <param name="input">The input.</param>
- /// <param name="pattern">The pattern.</param>
- /// <returns></returns>
- public static bool SQLLike(this string input, string pattern)
- {
- bool isMatch = true,
- isWildCardOn = false,
- isCharWildCardOn = false,
- isCharSetOn = false,
- isNotCharSetOn = false,
- endOfPattern = false;
- int lastWildCard = -1;
- int patternIndex = 0;
- List<char> set = new List<char>();
- char p = '\0';
- for (int i = 0; i < input.Length; i++)
- {
- char c = input[i];
- endOfPattern = (patternIndex >= pattern.Length);
- if (!endOfPattern)
- {
- p = pattern[patternIndex];
- if (!isWildCardOn && p == '%')
- {
- lastWildCard = patternIndex;
- isWildCardOn = true;
- while (patternIndex < pattern.Length &&
- pattern[patternIndex] == '%')
- {
- patternIndex++;
- }
- if (patternIndex >= pattern.Length) p = '\0';
- else p = pattern[patternIndex];
- }
- else if (p == '_')
- {
- isCharWildCardOn = true;
- patternIndex++;
- }
- else if (p == '[')
- {
- if (pattern[++patternIndex] == '^')
- {
- isNotCharSetOn = true;
- patternIndex++;
- }
- else isCharSetOn = true;
- set.Clear();
- if (pattern[patternIndex + 1] == '-' && pattern[patternIndex + 3] == ']')
- {
- char start = char.ToUpper(pattern[patternIndex]);
- patternIndex += 2;
- char end = char.ToUpper(pattern[patternIndex]);
- if (start <= end)
- {
- for (char ci = start; ci <= end; ci++)
- {
- set.Add(ci);
- }
- }
- patternIndex++;
- }
- while (patternIndex < pattern.Length &&
- pattern[patternIndex] != ']')
- {
- set.Add(pattern[patternIndex]);
- patternIndex++;
- }
- patternIndex++;
- }
- }
- if (isWildCardOn)
- {
- if (char.ToUpper(c) == char.ToUpper(p))
- {
- isWildCardOn = false;
- patternIndex++;
- }
- }
- else if (isCharWildCardOn)
- {
- isCharWildCardOn = false;
- }
- else if (isCharSetOn || isNotCharSetOn)
- {
- bool charMatch = (set.Contains(char.ToUpper(c)));
- if ((isNotCharSetOn && charMatch) || (isCharSetOn && !charMatch))
- {
- if (lastWildCard >= 0) patternIndex = lastWildCard;
- else
- {
- isMatch = false;
- break;
- }
- }
- isNotCharSetOn = isCharSetOn = false;
- }
- else
- {
- if (char.ToUpper(c) == char.ToUpper(p))
- {
- patternIndex++;
- }
- else
- {
- if (lastWildCard >= 0) patternIndex = lastWildCard;
- else
- {
- isMatch = false;
- break;
- }
- }
- }
- }
- endOfPattern = (patternIndex >= pattern.Length);
- if (isMatch && !endOfPattern)
- {
- bool isOnlyWildCards = true;
- for (int i = patternIndex; i < pattern.Length; i++)
- {
- if (pattern[i] != '%')
- {
- isOnlyWildCards = false;
- break;
- }
- }
- if (isOnlyWildCards) endOfPattern = true;
- }
- return isMatch && endOfPattern;
- }
- }
- }
|