Determine if a string is numeric: Difference between revisions

Content added Content deleted
Line 54: Line 54:
}
}
}
}

Alternative 1 : Check that each character in the string is number


private static final boolean isNumeric(final String s) {
for (int x = 0; x < s.length(); x++) {
final char c = s.charAt(x);
if (x == 0 && (c == '-')) continue; // negative
if ((c >= '0') && (c <= '9')) continue; // 0 - 9
return false; // invalid
}
return true; // valid
}

Alternative 2 : use a regular expression ( a more elegant solution)

public static boolean IsNumeric(string inputData) {
final static Regex isNumber = new Regex(@"^\d+$");
Match m = isNumber.Match(inputData);
return m.Success;
}


==[[mIRC]]==
==[[mIRC]]==