Whether a string is a percentage, ie if it finishes by '%' and the rest is a number fraction - CSharp System

CSharp examples for System:String Number

Description

Whether a string is a percentage, ie if it finishes by '%' and the rest is a number fraction

Demo Code


using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Text;
using System.Net.Mail;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;//from  w  w  w . j  a v a  2  s . co  m

public class Main{
        /// <summary>
        /// Whether a string is a percentage, 
        /// ie if it finishes by '%' and the rest is a number/fraction
        /// </summary>
        public static bool IsPercentage(string input)
        {
            return !string.IsNullOrEmpty(input)
                   && input.Last() == '%'
                   && (IsNumber(input.Substring(0, input.Length - 1)) || IsFraction(input.Substring(0, input.Length - 1)));
        }
}

Related Tutorials