Added stripTrainingNonDigits to MathUtil.

This commit is contained in:
nathan 2017-07-04 19:10:04 +02:00
parent 1f3bbcf76b
commit 1dca0e63dc

View File

@ -190,6 +190,34 @@ class MathUtil {
}
}
/**
* Removes any characters from the end that are not a number
*
* @param text the input text that may, or may not, contain a mix of numbers and letters
* @return the value as an integer
*/
public static
int stripTrailingNonDigits(final String text) {
if (text == null || text.isEmpty()) {
return 0;
}
int numberIndex = 0;
int length = text.length();
while (numberIndex < length && Character.isDigit(text.charAt(numberIndex))) {
numberIndex++;
}
String substring = text.substring(0, numberIndex);
try {
return Integer.parseInt(substring);
} catch (Exception ignored) {
}
return 0;
}
public static
boolean isEven(int value) {
return (value & 1) == 0;