CSharp examples for System:String Match
Allow only one decimal point
using System.Text; using System.Globalization; using System.Collections.Specialized; using System.Collections; using System;//from ww w .j a v a2 s. com public class Main{ /// <summary> /// Allow only one decimal point, used for IsNumericFloat. /// </summary> /// <param name="str">Input string to check</param> /// <param name="numberFormat">Used number format, e.g. /// CultureInfo.InvariantCulture.NumberFormat</param> /// <return>True if check succeeded, false otherwise</return> private static bool AllowOnlyOneDecimalPoint( string str, NumberFormatInfo numberFormat ) { char[] strInChars = str.ToCharArray(); bool hasGroupSeperator = false; int decimalSeperatorCount = 0; for ( int i = 0; i < strInChars.Length; i++ ) { if ( numberFormat.CurrencyDecimalSeparator.IndexOf( strInChars[ i ] ) == 0 ) { decimalSeperatorCount++; } // if (numberFormat.CurrencyDecimalSeparator.IndexOf) // has float group seperators ? if ( numberFormat.CurrencyGroupSeparator.IndexOf( strInChars[ i ] ) == 0 ) { hasGroupSeperator = true; } // if (numberFormat.CurrencyGroupSeparator.IndexOf) } // for (int) if ( hasGroupSeperator ) { // If first digit is the group seperator or begins with 0, // there is something wrong, the group seperator is used as a comma. if ( str.StartsWith( numberFormat.CurrencyGroupSeparator ) || strInChars[ 0 ] == '0' ) return false; // look only at the digits in front of the decimal point string[] splittedByDecimalSeperator = str.Split( numberFormat.CurrencyDecimalSeparator.ToCharArray() ); #region Invert the digits for modulo check // ==> 1.000 -> 000.1 ==> only after 3 digits char[] firstSplittedInChars = splittedByDecimalSeperator[ 0 ].ToCharArray(); int arrayLength = firstSplittedInChars.Length; char[] firstSplittedInCharsInverted = new char[ arrayLength ]; for ( int i = 0; i < arrayLength; i++ ) { firstSplittedInCharsInverted[ i ] = firstSplittedInChars[ arrayLength - 1 - i ]; } // for (int) #endregion // group seperators are only allowed between 3 digits -> 1.000.000 for ( int i = 0; i < arrayLength; i++ ) { if ( i % 3 != 0 && numberFormat.CurrencyGroupSeparator.IndexOf( firstSplittedInCharsInverted[ i ] ) == 0 ) { return false; } // if (i) } // for (int) } // if (hasGroupSeperator) if ( decimalSeperatorCount > 1 ) return false; return true; } // AllowOnlyOneDecimalPoint(str, numberFormat) }