2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.string;
12 import java.util.Vector;
14 public class String2 {
16 private final Vector<Character> chars = new Vector<Character>();
18 public String2(final String value) {
19 for (final Character c : value.toCharArray())
23 public String2 addPrefix(final String prefix) {
25 for (final char c : prefix.toCharArray())
26 chars.insertElementAt(c, j++);
31 public String2 addSuffix(final String suffix) {
32 for (final char c : suffix.toCharArray())
39 * Cut given amount of characters from the left of the string. Return cutted
42 public String cutLeft(final int cutAmount) {
44 int actualCutAmount = cutAmount;
46 if (actualCutAmount > getLength())
47 actualCutAmount = getLength();
49 final String result = getSubString(0, actualCutAmount);
51 chars.subList(0, actualCutAmount).clear();
56 public void enforceLength(final int targetLength) {
57 if (getLength() > targetLength)
58 chars.subList(targetLength, getLength()).clear();
59 else if (getLength() < targetLength) {
60 final int charactersToAdd = targetLength - getLength();
61 for (int i = 0; i < charactersToAdd; i++)
66 public int getLength() {
70 public String getSubString(final int startInclusive, final int endExclusive) {
71 final char[] charArray = new char[endExclusive - startInclusive];
74 for (int i = startInclusive; i < endExclusive; i++) {
75 charArray[j] = chars.get(i);
78 return new String(charArray);
81 public boolean isEmpty() {
82 return chars.size() == 0;
86 public String toString() {
87 return getSubString(0, chars.size());