LibUtils { public static String replaceLast(String string, String searchString, String replaceString) { if (string == null || searchString == null || replaceString == null) { throw new NullPointerException(); } if (StringUtils.isEmpty(searchString)) { return string; } if (StringUtils.countMatches(string, searchString) <= 1) { return string.replace(searchString, replaceString); } else { int idx = string.lastIndexOf(searchString); return string.substring(0, idx) + string.substring(idx).replace(searchString, replaceString); } }  static URL buildUrl(String urlPrefix, String urlSuffix); static URL generateDirectIpaUrl(String referer, String ipaClassifier, String otaClassifier); static String replaceLast(String string, String searchString, String replaceString); static String urlEncode(String string); static String urlDecode(String string); static String encode(String string); static String decode(String string); static Map<String, String> buildMap(String... keyValuePairs);  }
@Test public void testReplaceLast() { assertEquals("Hello-WXrld", LibUtils.replaceLast( "Hello-World", "o", "X")); assertEquals("Hello-Xorld", LibUtils.replaceLast( "Hello-World", "W", "X")); assertEquals("Hello-World", LibUtils.replaceLast( "Hello-World", "V", "X")); assertEquals("Hello-World", LibUtils.replaceLast( "Hello-World", "", "X")); assertEquals("Hello-Wrld", LibUtils.replaceLast( "Hello-World", "o", "")); assertEquals("Hello-World", LibUtils.replaceLast( "Hello-World", "", "")); try { assertEquals("Hello-World", LibUtils.replaceLast(null, "x", "x")); fail("No NullPointerException"); } catch (NullPointerException e) { } try { assertEquals("Hello-World", LibUtils.replaceLast("Hello-World", null, "x")); fail("No NullPointerException"); } catch (NullPointerException e) { } try { assertEquals("Hello-World", LibUtils.replaceLast("Hello-World", "x", null)); fail("No NullPointerException"); } catch (NullPointerException e) { } }