Trim left spaces of StringBuilder - CSharp System

CSharp examples for System:String Strip

Description

Trim left spaces of StringBuilder

Demo Code


using System.Text;
using System;/* www.  j  av a 2 s.co  m*/

public class Main{
        /// <summary>
        /// Trim left spaces of string
        /// </summary>
        /// <param name="sb">StringBuilder object</param>
        /// <returns>Returns a new stringbuilder object</returns>
        public static StringBuilder LTrim(this StringBuilder sb)
        {
            Validation.IsNotNull(sb, "sb");
            Validation.Validate(sb.Length > 0);

            var length = 0;
            var num2 = sb.Length;
            while ((sb[length] == ' ') && (length < num2))
                length++;

            if (length > 0)
                sb.Remove(0, length);

            return sb;
        }
        /// <summary>
        /// Removes the indicated character from the stringbuilder object
        /// </summary>
        /// <param name="sb">StringBuilder object</param>
        /// <param name="value">Character to remove</param>
        /// <returns>Returns a new stringbuilder object</returns>
        public static StringBuilder Remove(this StringBuilder sb, char value)
        {
            Validation.IsNotNull(sb, "sb");
            Validation.Validate(sb.Length > 0);

            for (var i = 0; i < sb.Length; )
            {
                if (sb[i] == value)
                    sb.Remove(i, 1);
                else
                    i++;
            }
            return sb;
        }
}

Related Tutorials