Esempio n. 1
0
        /// <summary>
        /// Highights all occurrances of the given string with the desired foreground and background color.
        /// </summary>
        /// <param name="toFind">The substring to find</param>
        /// <param name="foregroundColor">The foreground color (defaults to the console's foreground color at initialization time).</param>
        /// <param name="backgroundColor">The background color (defaults to the console's background color at initialization time).</param>
        /// <param name="comparison">Specifies how characters are compared</param>
        /// <returns>A new ConsoleString with the highlights.</returns>
        public ConsoleString Highlight(string toFind, ConsoleColor?foregroundColor = null, ConsoleColor?backgroundColor = null, StringComparison comparison = StringComparison.InvariantCulture)
        {
            ConsoleString ret = new ConsoleString(this);

            if (toFind == null || toFind.Length == 0)
            {
                return(ret);
            }

            int startIndex = 0;

            while (true)
            {
                string toString     = ret.ToString();
                int    currentIndex = toString.IndexOf(toFind, startIndex, comparison);
                if (currentIndex < 0)
                {
                    break;
                }

                string replacement = "";
                for (int i = 0; i < toFind.Length; i++)
                {
                    replacement += ret.characters[currentIndex].Value;
                    ret.characters.RemoveAt(currentIndex);
                }
                ret.characters.InsertRange(currentIndex, replacement.Select(c => new ConsoleCharacter(c, foregroundColor, backgroundColor)));
                startIndex = currentIndex + replacement.Length;
            }

            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Replaces all occurrances of the given string with the replacement value using the specified formatting.
        /// </summary>
        /// <param name="toFind">The substring to find</param>
        /// <param name="toReplace">The replacement value</param>
        /// <param name="foregroundColor">The foreground color (defaults to the console's foreground color at initialization time).</param>
        /// <param name="backgroundColor">The background color (defaults to the console's background color at initialization time).</param>
        /// <returns>A new ConsoleString with the replacements.</returns>
        public ConsoleString Replace(string toFind, string toReplace, ConsoleColor?foregroundColor = null, ConsoleColor?backgroundColor = null)
        {
            ConsoleString ret = new ConsoleString();

            ret.Append(this);

            int startIndex = 0;

            while (true)
            {
                string toString     = ret.ToString();
                int    currentIndex = toString.IndexOf(toFind, startIndex);
                if (currentIndex < 0)
                {
                    break;
                }
                for (int i = 0; i < toFind.Length; i++)
                {
                    ret.RemoveAt(currentIndex);
                }
                ret.InsertRange(currentIndex, toReplace.Select(c => new ConsoleCharacter(c, foregroundColor, backgroundColor)));
                startIndex = currentIndex + toReplace.Length;
            }

            return(ret);
        }