X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;h=4c1627f6aee3bd63cfe37f52639f3dc38bfcfb85;hb=96b417fd35bb6aae9e5e57af94cecb44aeeb9c22;hp=b19e99a74e997c13e9138c4d498b1470e092fea7;hpb=2ee43de10e5311ba64e7fc855e253d02326e254c;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 b19e99a..4c1627f 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -1,7 +1,7 @@ /* * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * * This program is free software; you can redistribute it and/or * modify it under the terms of version 3 of the GNU Lesser General Public License * or later as published by the Free Software Foundation. @@ -9,83 +9,143 @@ 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) { - for (final Character c : value.toCharArray()) - chars.add(c); - } + public String2(String value) { + if (value == null) + value = ""; - public String2 addPrefix(final String prefix) { - int j = 0; - for (final char c : prefix.toCharArray()) - chars.insertElementAt(c, j++); + chars = new ArrayList<>(value.length()); + for (final Character c : value.toCharArray()) + chars.add(c); + } - return this; - } + public String2 addPrefix(final String prefix) { + if (prefix == null) + return this; - public String2 addSuffix(final String suffix) { - for (final char c : suffix.toCharArray()) - chars.add(c); + int i = 0; + for (final char c : prefix.toCharArray()) + chars.add(i++, c); - return this; - } + return this; + } - /** - * Cut given amount of characters from the left of the string. Return cutted - * part. - */ - public String cutLeft(final int cutAmount) { + public String2 addSuffix(final String suffix) { + if (suffix == null) + return this; - int actualCutAmount = cutAmount; + for (final char c : suffix.toCharArray()) + chars.add(c); - if (actualCutAmount > getLength()) - actualCutAmount = getLength(); + return this; + } - final String result = getSubString(0, actualCutAmount); + /** + * Cut given amount of characters from the left of the string. + * + * @param cutAmount of characters to cut + * @return part that was cut. + */ + public String2 trimPrefix(final int cutAmount) { - chars.subList(0, actualCutAmount).clear(); + int actualCutAmount = cutAmount; - return result; - } + if (actualCutAmount > getLength()) + actualCutAmount = getLength(); - public String2 enforceLength(final int targetLength) { - if (getLength() > targetLength) - chars.subList(targetLength, getLength()).clear(); - else if (getLength() < targetLength) { - final int charactersToAdd = targetLength - getLength(); - for (int i = 0; i < charactersToAdd; i++) - chars.add(' '); - } + chars.subList(0, actualCutAmount).clear(); - return this; - } + return this; + } - public int getLength() { - return chars.size(); - } + public String2 trimPrefixIfExists(String prefix){ + if (prefix == null) + return this; - public String getSubString(final int startInclusive, final int endExclusive) { - final char[] charArray = new char[endExclusive - startInclusive]; + if (hasPrefix(prefix)) + trimPrefix(prefix.length()); - int j = 0; - for (int i = startInclusive; i < endExclusive; i++) { - charArray[j] = chars.get(i); - j++; - } - return new String(charArray); - } + return this; + } - public boolean isEmpty() { - return chars.size() == 0; - } + public String2 trimSuffixIfExists(String suffix){ + if (hasSuffix(suffix)) + trimSuffix(suffix.length()); - @Override - public String toString() { - return getSubString(0, chars.size()); - } + 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) { + if (getLength() > targetLength) + chars.subList(targetLength, getLength()).clear(); + else if (getLength() < targetLength) { + final int charactersToAdd = targetLength - getLength(); + for (int i = 0; i < charactersToAdd; i++) + chars.add(' '); + } + + return this; + } + + public int getLength() { + return chars.size(); + } + + public String getSubString(final int startInclusive, final int endExclusive) { + final char[] charArray = new char[endExclusive - startInclusive]; + + int j = 0; + for (int i = startInclusive; i < endExclusive; i++) { + charArray[j] = chars.get(i); + j++; + } + return new String(charArray); + } + + public boolean isEmpty() { + return chars.size() == 0; + } + + @Override + public String toString() { + return getSubString(0, chars.size()); + } }