From 1dca0e63dc0db6c0480947e09f83821643ed8632 Mon Sep 17 00:00:00 2001 From: nathan Date: Tue, 4 Jul 2017 19:10:04 +0200 Subject: [PATCH] Added stripTrainingNonDigits to MathUtil. --- src/dorkbox/util/MathUtil.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/dorkbox/util/MathUtil.java b/src/dorkbox/util/MathUtil.java index 98bf00f..1401da4 100644 --- a/src/dorkbox/util/MathUtil.java +++ b/src/dorkbox/util/MathUtil.java @@ -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;