Ejemplo n.º 1
0
        public static SingleReport Replace(string input, string search, string replace, bool ignoreCase)
        {
            SingleReport singleReport = new SingleReport();

            if (input.Length == 0 || search.Length == 0 || search.Length > input.Length)
            {
                singleReport.Text = input;
            }
            else
            {
                StringBuilder resultString = new StringBuilder(input.Length);

                int pos = 0;
                while (pos + search.Length <= input.Length)
                {
                    if (string.Compare(input, pos, search, 0, search.Length, ignoreCase) == 0)
                    {
                        // add the replaced string
                        resultString.Append(replace);
                        pos += search.Length;

                        ++singleReport.Matches;
                    }
                    else
                    {
                        // advance one character
                        resultString.Append(input, pos++, 1);
                    }
                }

                // append remaining characters
                resultString.Append(input, pos, input.Length - pos);

                singleReport.Text = resultString.ToString();
            }

            return singleReport;
        }
Ejemplo n.º 2
0
        public static SingleReport Replace(string input, string search, string replace, bool ignoreCase)
        {
            SingleReport singleReport = new SingleReport();

            MatchCollection matches = ignoreCase ?
                Regex.Matches(input, search, RegexOptions.IgnoreCase) :
                Regex.Matches(input, search);

            singleReport.Matches = matches.Count;

            if (matches.Count == 0)
            {
                singleReport.Text = input;
            }
            else
            {
                singleReport.Text = ignoreCase ?
                    Regex.Replace(input, search, replace, RegexOptions.IgnoreCase) :
                    Regex.Replace(input, search, replace);
            }

            return singleReport;
        }