X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;h=b6083edaa61dae51dd6103d2403b960cd4dacfde;hb=7139ae895dd50d9dd1cbd2b3110c93f546cef798;hp=504c41d876a2ff24c52e18e17a27d4a63a2195ce;hpb=1195d9ce07d2e4f2d4c2a7d67dc0bd657556e2c7;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index 504c41d..b6083ed 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -14,15 +14,16 @@ import java.util.List; public class String2 { - private final List chars; + private final List chars = new ArrayList<>(); - public String2(final String value) { - chars = new ArrayList<>(value.length()); - for (final Character c : value.toCharArray()) - chars.add(c); + public String2(String value) { + addSuffix(value); } public String2 addPrefix(final String prefix) { + if (prefix == null) + return this; + int i = 0; for (final char c : prefix.toCharArray()) chars.add(i++, c); @@ -31,12 +32,23 @@ public class String2 { } 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. * @@ -56,6 +68,9 @@ public class String2 { } public String2 trimPrefixIfExists(String prefix){ + if (prefix == null) + return this; + if (hasPrefix(prefix)) trimPrefix(prefix.length());