X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;fp=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;h=fe83adef74852f91cc4332f9be48537475aa13af;hb=a10b74a05e9aecc78e1e983025d1933b91e076da;hp=0000000000000000000000000000000000000000;hpb=362481efcdbdebb9c6c65490bfcdc450c00cc778;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 new file mode 100755 index 0000000..fe83ade --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -0,0 +1,89 @@ +/* + * Svjatoslav Commons - shared library of common functionality. + * Copyright ©2012-2014, 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. + */ + +package eu.svjatoslav.commons.string; + +import java.util.Vector; + +public class String2 { + + private final Vector chars = new Vector(); + + public String2(final String value) { + for (final Character c : value.toCharArray()) + chars.add(c); + } + + public String2 addPrefix(final String prefix) { + int j = 0; + for (final char c : prefix.toCharArray()) + chars.insertElementAt(c, j++); + + return this; + } + + public String2 addSuffix(final String suffix) { + for (final char c : suffix.toCharArray()) + chars.add(c); + + return this; + } + + /** + * Cut given amount of characters from the left of the string. Return cutted + * part. + */ + public String cutLeft(final int cutAmount) { + + int actualCutAmount = cutAmount; + + if (actualCutAmount > getLength()) + actualCutAmount = getLength(); + + final String result = getSubString(0, actualCutAmount); + + chars.subList(0, actualCutAmount).clear(); + + return result; + } + + public void 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(' '); + } + } + + 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()); + } +}