From: Svjatoslav Agejenko Date: Fri, 13 Oct 2017 09:36:11 +0000 (+0300) Subject: Improved suffix and prefix handling in String2. X-Git-Tag: svjatoslavcommons-1.8~46 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=1195d9ce07d2e4f2d4c2a7d67dc0bd657556e2c7;p=svjatoslav_commons.git Improved suffix and prefix handling in String2. --- diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index 113f67e..504c41d 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -9,21 +9,23 @@ package eu.svjatoslav.commons.string; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; public class String2 { - private final Vector chars = new Vector<>(); + private final List chars; public String2(final String value) { + chars = new ArrayList<>(value.length()); for (final Character c : value.toCharArray()) chars.add(c); } public String2 addPrefix(final String prefix) { - int j = 0; + int i = 0; for (final char c : prefix.toCharArray()) - chars.insertElementAt(c, j++); + chars.add(i++, c); return this; } @@ -41,18 +43,62 @@ public class String2 { * @param cutAmount of characters to cut * @return part that was cut. */ - public String trimLeft(final int cutAmount) { + public String2 trimPrefix(final int cutAmount) { int actualCutAmount = cutAmount; if (actualCutAmount > getLength()) actualCutAmount = getLength(); - final String result = getSubString(0, actualCutAmount); - chars.subList(0, actualCutAmount).clear(); - return result; + return this; + } + + public String2 trimPrefixIfExists(String prefix){ + if (hasPrefix(prefix)) + trimPrefix(prefix.length()); + + return this; + } + + public String2 trimSuffixIfExists(String suffix){ + if (hasSuffix(suffix)) + trimSuffix(suffix.length()); + + return this; + } + + public String2 trimSuffix(int charsToTrim) { + + if (charsToTrim > chars.size()){ + chars.clear(); + return this; + } + + for (int i = 0; i chars.size()) + return false; + + for (int i = 0; i < fragment.length(); i++) + if (chars.get(index + i) != fragment.charAt(i)) + return false; + + return true; } public String2 enforceLength(final int targetLength) { diff --git a/src/test/java/eu/svjatoslav/commons/string/String2Test.java b/src/test/java/eu/svjatoslav/commons/string/String2Test.java index 35e203b..adc14a8 100755 --- a/src/test/java/eu/svjatoslav/commons/string/String2Test.java +++ b/src/test/java/eu/svjatoslav/commons/string/String2Test.java @@ -16,18 +16,28 @@ import static org.junit.Assert.assertEquals; public class String2Test { @Test - public void testCutLeft() { + public void testTrimPrefix() { final String2 s = new String2("this is a test"); - assertEquals("this ", s.trimLeft(5)); - assertEquals("is ", s.trimLeft(3)); - assertEquals("a ", s.trimLeft(2)); - assertEquals("test", s.trimLeft(25)); - assertEquals("", s.trimLeft(5)); + assertEquals("is a test", s.trimPrefix(5).toString()); + assertEquals("a test", s.trimPrefix(3).toString()); + assertEquals("test", s.trimPrefix(2).toString()); + assertEquals("", s.trimPrefix(500).toString()); + } + + @Test + public void testTrimSuffix() { + final String2 s = new String2("this is a test"); + + assertEquals("this is a", s.trimSuffix(5).toString()); + assertEquals("this is", s.trimSuffix(2).toString()); + assertEquals("this", s.trimSuffix(3).toString()); + assertEquals("", s.trimSuffix(500).toString()); } + @Test public void testEnforceLength() { final String2 s = new String2("12345678");