Exemple #1
0
 /// <summary>
 /// Returns a new string with all occurrences of a specified substring replaced
 /// </summary>
 /// <param name="str"></param>
 /// <param name="oldValue">The string to be replaced</param>
 /// <param name="newValue">The string to replace all occurances of oldValue</param>
 /// <param name="comparisonType">One of the enumeration values that specifies the rules for the search</param>
 /// <returns></returns>
 public static string Replace(this string str, string oldValue, string newValue, StringComparison comparisonType = UseCase)
 {
     if (oldValue.In(new string[] { "\\" }))
     {
         return(str.Replace(oldValue, newValue));
     }
     return(Regex.Replace(str, Regex.Escape(oldValue), newValue, comparisonType.ToRegEx()));
 }
Exemple #2
0
        /// <summary>
        /// Checks if a string matches pattern
        /// </summary>
        /// <param name="str"></param>
        /// <param name="patternValue">The pattern to compare: *=any characters, ?=a character, #=a digit, etc.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the search</param>
        /// <returns></returns>
        public static bool Like(this string str, string patternValue, StringComparison comparisonType = UseCase)
        {
            int    Index        = 0;
            string Ch           = "";
            string regexPattern = "";

            // Escape parens
            patternValue = patternValue.Replace("(", @"\(");
            patternValue = patternValue.Replace(")", @"\)");

            // Convert Pattern from a like operand to a regular expression
            while (Index < patternValue.Length)
            {
                Ch = patternValue.Substring(Index, 1);

                // Ignore escaped literals
                if (Ch == "[")
                {
                    while (Ch != "]" & Index <= patternValue.Length)
                    {
                        regexPattern += Ch;
                        Index        += 1;
                        Ch            = patternValue.Substring(Index, 1);
                    }
                }

                // Convert wildcards
                switch (Ch)
                {
                case "*":
                case "%":
                    regexPattern += "(.|\n)*";
                    break;

                case "?":
                case "_":
                    regexPattern += ".{1}";
                    break;

                case "#":
                    regexPattern += "[\\d]+";
                    break;

                default:
                    regexPattern += Ch;
                    break;
                }
                Index += 1;
            }

            // Convert character exclusion syntax
            regexPattern = regexPattern.Replace("[!", "[^");
            if (comparisonType.ToRegEx() == RegexOptions.IgnoreCase)
            {
                regexPattern = "(?i)" + regexPattern;
            }

            MatchCollection Matches = Regex.Matches(str, regexPattern, RegexOptions.Multiline & comparisonType.ToRegEx());

            if (Matches.Count > 0)
            {
                return(Matches[0].Value.Equals(str, comparisonType));
            }
            return(false);
        }
Exemple #3
0
 /// <summary>
 /// Returns a value indicating whether a specified values occurs within this string.
 /// </summary>
 /// <param name="str"></param>
 /// <param name="Value">Substring to search for</param>
 /// <param name="comparisonType">One of the enumeration values that specifies the rules for the search</param>
 /// <returns></returns>
 public static bool Contains(this string str, string Value, StringComparison comparisonType = UseCase)
 {
     return(Regex.IsMatch(str, Regex.Escape(Value), comparisonType.ToRegEx()));
 }