Removes the leading zeros. - CSharp System

CSharp examples for System:String Strip

Description

Removes the leading zeros.

Demo Code


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;/* www.j  a v  a 2s .c o m*/

public class Main{
        /// <summary>
        /// Removes the leading zeros.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string RemoveLeadingZeros(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return value;
            value = value.TrimStart();            
            int count = 0;
            for (int i = 0; i < value.Length; i++)
            {
                if (value[i].Equals('0')) ++count;
                else break;
            }
            if (count > 0) value = value.Substring(count);
            return value;
        }
}

Related Tutorials