CssLengthUtil { public static Object[] getLengthValueAndUnit(final String cssValue) { final String trimmedCssValue = StringUtil.strip(cssValue); final char[] cssValueChars = trimmedCssValue.toCharArray(); int lengthSeparationIndex = -1; for (int i = cssValueChars.length - 1; i > -1; i--) { final char c = cssValueChars[i]; if (!(c < '0' || c > '9')) { lengthSeparationIndex = i; break; } } final String value = StringUtil .strip(trimmedCssValue.substring(0, lengthSeparationIndex + 1)); try { if (lengthSeparationIndex == (cssValueChars.length - 1)) { return new Object[] { Float.valueOf(value) }; } String unit = StringUtil.strip( trimmedCssValue.substring(lengthSeparationIndex + 1)); if (unit.length() == 1 && unit.charAt(0) == '%') { return new Object[] { Float.valueOf(value), CssLengthUnit.PER }; } else { unit = unit.toUpperCase(); } return new Object[] { Float.valueOf(value), CssLengthUnit.valueOf(unit) }; } catch (final IllegalArgumentException e) { return new Object[0]; } } private  CssLengthUtil(); static Object[] getLengthValueAsPremitiveAndUnit(
            final String cssValue); static Object[] getLengthValueAndUnit(final String cssValue);  }
@Test public void testGetLengthValueAndUnit() { assertEquals(2, CssLengthUtil.getLengthValueAndUnit("2px").length); assertEquals(5.0F, CssLengthUtil.getLengthValueAndUnit("5px")[0]); assertEquals(CssLengthUnit.PX, CssLengthUtil.getLengthValueAndUnit("5px")[1]); assertEquals(1, CssLengthUtil.getLengthValueAndUnit("2").length); assertEquals(15F, CssLengthUtil.getLengthValueAndUnit("15")[0]); assertEquals(25F, CssLengthUtil.getLengthValueAndUnit(" 25 % ")[0]); assertEquals(CssLengthUnit.PER, CssLengthUtil.getLengthValueAndUnit(" 25 % ")[1]); }