示例#1
0
        /// <summary>
        /// Remove all leading, trailing, or both, occurences of the specified char.
        /// </summary>
        public static string Strip(this string input, StripMode mode, string character)
        {
            int    ptr    = 0;
            string output = string.Empty;

            //Parameter validation.
            if (input.Length == 0)
            {
                return(input);
            }
            switch (mode)
            {
            case StripMode.Both:
                break;

            case StripMode.Leading:
                break;

            case StripMode.Trailing:
                break;

            default:
                return(input);
            }
            if (character.Length != 1)
            {
                return(input);
            }
            //Main processing.
            if (mode == StripMode.Leading || mode == StripMode.Both)
            {
                for (ptr = 0; ptr < input.Length; ptr++)
                {
                    if (input.Substring(ptr, 1) != character)
                    {
                        output = input.Substring(ptr);
                        break;
                    }
                }
                input = output;
            }
            if (mode == StripMode.Trailing || mode == StripMode.Both)
            {
                for (ptr = input.Length - 1; ptr >= 0; ptr--)
                {
                    if (input.Substring(ptr, 1) != character)
                    {
                        output = input.Substring(0, ptr + 1);
                        break;
                    }
                }
            }
            return(output);
        }
示例#2
0
        private string Strip(string input, StripMode mode, string character)
        {
            int    ptr    = 0;
            string output = string.Empty;

            if (input.Length == 0)
            {
                return(input);
            }
            switch (mode)
            {
            case StripMode.Both:
                break;

            case StripMode.Leading:
                break;

            case StripMode.Trailing:
                break;

            default:
                return(input);
            }
            if (character.Length != 1)
            {
                return(input);
            }
            if (mode == StripMode.Leading || mode == StripMode.Both)
            {
                for (ptr = 0; ptr < input.Length; ptr++)
                {
                    if (input.Substring(ptr, 1) != character)
                    {
                        output = input.Substring(ptr);
                        break;
                    }
                }
                input = output;
            }
            if (mode == StripMode.Trailing || mode == StripMode.Both)
            {
                for (ptr = input.Length - 1; ptr >= 0; ptr--)
                {
                    if (input.Substring(ptr, 1) != character)
                    {
                        output = input.Substring(0, ptr + 1);
                        break;
                    }
                }
            }
            return(output);
        }