Browse Source

* Test for removeSuffix()

* Test + Impl of removePrefix()

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@577781 13f79535-47bb-0310-9956-ffa450edef68
master
Jan Materne 17 years ago
parent
commit
2ed29da51a
2 changed files with 49 additions and 0 deletions
  1. +15
    -0
      src/main/org/apache/tools/ant/util/StringUtils.java
  2. +34
    -0
      src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java

+ 15
- 0
src/main/org/apache/tools/ant/util/StringUtils.java View File

@@ -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 <i>string</i> with the <i>prefix</i>
*/
public static String removePrefix(String string, String prefix) {
if (string.startsWith(prefix)) {
return string.substring(prefix.length());
} else {
return string;
}
}
}

+ 34
- 0
src/tests/junit/org/apache/tools/ant/util/StringUtilsTest.java View File

@@ -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")
);
}
}

Loading…
Cancel
Save