diff --git a/src/main/org/apache/tools/ant/util/StringUtils.java b/src/main/org/apache/tools/ant/util/StringUtils.java index 74c8adfc9..9e534f236 100644 --- a/src/main/org/apache/tools/ant/util/StringUtils.java +++ b/src/main/org/apache/tools/ant/util/StringUtils.java @@ -256,4 +256,19 @@ public final class StringUtils { return string; } } + + /** + * Removes the prefix from a given string, if the string contains + * that prefix. + * @param string String for check + * @param prefix Prefix to remove + * @return the string with the prefix + */ + public static String removePrefix(String string, String prefix) { + if (string.startsWith(prefix)) { + return string.substring(prefix.length()); + } else { + return string; + } + } } diff --git a/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java index 7243b5280..52e089862 100644 --- a/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java @@ -117,4 +117,38 @@ public class StringUtilsTest extends TestCase { assertEquals(StringUtils.parseHumanSizes("1P"), PETABYTE); assertEquals(StringUtils.parseHumanSizes("1"), 1L); } + + public void testRemoveSuffix() { + String prefix = "Prefix"; + String name = "Name"; + String suffix = "Suffix"; + String input = prefix + name + suffix; + assertEquals( + "Does not remove the suffix right.", + prefix + name, + StringUtils.removeSuffix(input, suffix) + ); + assertEquals( + "Should leave the string unattended.", + prefix + name + suffix, + StringUtils.removeSuffix(input, "bla") + ); + } + + public void testRemovePrefix() { + String prefix = "Prefix"; + String name = "Name"; + String suffix = "Suffix"; + String input = prefix + name + suffix; + assertEquals( + "Does not remove the prefix right.", + name + suffix, + StringUtils.removePrefix(input, prefix) + ); + assertEquals( + "Should leave the string unattended.", + prefix + name + suffix, + StringUtils.removePrefix(input, "bla") + ); + } }