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=37ee8e9b2b4b58ab1e2176dcf25d274d35576f30;hp=fe83adef74852f91cc4332f9be48537475aa13af;hb=9bf004ce4e9b5edff36c65fcc8cc0f303390d7fc;hpb=a10b74a05e9aecc78e1e983025d1933b91e076da diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index fe83ade..37ee8e9 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 - * + * * 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. @@ -13,77 +13,81 @@ import java.util.Vector; public class String2 { - private final Vector chars = new Vector(); + private final Vector chars = new Vector<>(); - public String2(final String value) { - for (final Character c : value.toCharArray()) - chars.add(c); - } + 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++); + public String2 addPrefix(final String prefix) { + int j = 0; + for (final char c : prefix.toCharArray()) + chars.insertElementAt(c, j++); - return this; - } + return this; + } - public String2 addSuffix(final String suffix) { - for (final char c : suffix.toCharArray()) - chars.add(c); + public String2 addSuffix(final String suffix) { + for (final char c : suffix.toCharArray()) + chars.add(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) { + /** + * 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) { - int actualCutAmount = cutAmount; + int actualCutAmount = cutAmount; - if (actualCutAmount > getLength()) - actualCutAmount = getLength(); + if (actualCutAmount > getLength()) + actualCutAmount = getLength(); - final String result = getSubString(0, actualCutAmount); + final String result = getSubString(0, actualCutAmount); - chars.subList(0, actualCutAmount).clear(); + chars.subList(0, actualCutAmount).clear(); - return result; - } + 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 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(' '); + } - public int getLength() { - return chars.size(); - } + return this; + } - public String getSubString(final int startInclusive, final int endExclusive) { - final char[] charArray = new char[endExclusive - startInclusive]; + public int getLength() { + return chars.size(); + } - int j = 0; - for (int i = startInclusive; i < endExclusive; i++) { - charArray[j] = chars.get(i); - j++; - } - return new String(charArray); - } + public String getSubString(final int startInclusive, final int endExclusive) { + final char[] charArray = new char[endExclusive - startInclusive]; - public boolean isEmpty() { - return chars.size() == 0; - } + int j = 0; + for (int i = startInclusive; i < endExclusive; i++) { + charArray[j] = chars.get(i); + j++; + } + return new String(charArray); + } - @Override - public String toString() { - return getSubString(0, chars.size()); - } + public boolean isEmpty() { + return chars.size() == 0; + } + + @Override + public String toString() { + return getSubString(0, chars.size()); + } }