X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=svjatoslav_commons.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;h=9fb7baff57d500652099213c17d13d6f72d1ed9b;hp=113f67ea5ac5ed7bcec9cd2451f1f96dd2d6459a;hb=b8bd1e820265fc15c39c1ee8c06289ea8b8e2c1c;hpb=6846681d727a07385bcd3e0eb856f70a7e96448c diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index 113f67e..9fb7baf 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -9,50 +9,112 @@ 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 = new ArrayList<>(); - public String2(final String value) { - for (final Character c : value.toCharArray()) - chars.add(c); + public String2(String value) { + addSuffix(value); } public String2 addPrefix(final String prefix) { - int j = 0; + if (prefix == null) + return this; + + int i = 0; for (final char c : prefix.toCharArray()) - chars.insertElementAt(c, j++); + chars.add(i++, c); return this; } public String2 addSuffix(final String suffix) { + if (suffix == null) + return this; + for (final char c : suffix.toCharArray()) chars.add(c); return this; } + public String2 addSuffix(String separator, final String suffix) { + if (!isEmpty()) + addSuffix(separator); + + addSuffix(suffix); + + return this; + } + /** * Cut given amount of characters from the left of the string. * * @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 (prefix == null) + return this; + + 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 < charsToTrim; i++) + chars.remove(chars.size() - 1); + + return this; + } + + public boolean hasSuffix(String suffix) { + return contains(suffix, getLength() - suffix.length()); + } + + public boolean hasPrefix(String prefix) { + return contains(prefix, 0); + } + + public boolean contains(String fragment, int index) { + if (index + fragment.length() > 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) {