Trim String - CSharp System

CSharp examples for System:String Strip

Description

Trim String

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//from   w  w w.j  a v  a  2  s . c om

public class Main{
        public static string Trim(this string input, string trimString)
        {
            return input.TrimStart(trimString).TrimEnd(trimString);
        }
        public static string TrimStart(this string input, string trimString)
        {
            var result = input;
            while (result.StartsWith(trimString))
                result = result.Substring(trimString.Length);
            return result;
        }
        public static string TrimEnd(this string input, string trimString)
        {
            var result = input;
            while (result.EndsWith(trimString))
                result = result.Substring(0, result.Length - trimString.Length);
            return result;
        }
}

Related Tutorials