2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.string;
7 import java.util.ArrayList;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
12 public class String2 {
14 private final List<Character> chars = new ArrayList<>();
16 public String2 clear(){
21 public String2(String value) {
28 public String2 repeat(int count){
30 String value = toString();
32 for (int i = 1; i < count; i++)
40 public String2 prepend(final String prefix) {
45 for (final char c : prefix.toCharArray())
51 public String2 append(final String suffix) {
55 for (final char c : suffix.toCharArray())
61 public String2 appendWithSeparator(String separator, final String suffix) {
70 public String2 append(String s, int times) {
71 for (int i = 0; i < times; i++) append(s);
77 * Cut given amount of characters from the left of the string.
79 * @param cutAmount of characters to cut
80 * @return part that was cut.
82 public String2 trimPrefix(final int cutAmount) {
84 int actualCutAmount = cutAmount;
86 if (actualCutAmount > getLength())
87 actualCutAmount = getLength();
89 if (actualCutAmount > 0) chars.subList(0, actualCutAmount).clear();
94 public String2 trimPrefixIfExists(String prefix) {
98 if (hasPrefix(prefix))
99 trimPrefix(prefix.length());
104 public String2 trimSuffixIfExists(String suffix) {
105 if (hasSuffix(suffix))
106 trimSuffix(suffix.length());
111 public String2 trimSuffix(int charsToTrim) {
113 if (charsToTrim > chars.size()) {
118 for (int i = 0; i < charsToTrim; i++)
119 chars.remove(chars.size() - 1);
124 public boolean hasSuffix(String suffix) {
125 return contains(suffix, getLength() - suffix.length());
128 public boolean hasPrefix(String prefix) {
129 return contains(prefix, 0);
132 public boolean contains(String fragment, int index) {
133 if (index + fragment.length() > chars.size())
136 for (int i = 0; i < fragment.length(); i++)
137 if (chars.get(index + i) != fragment.charAt(i))
143 public String2 enforceLength(final int targetLength) {
144 if (getLength() > targetLength)
145 chars.subList(targetLength, getLength()).clear();
146 else if (getLength() < targetLength) {
147 final int charactersToAdd = targetLength - getLength();
148 for (int i = 0; i < charactersToAdd; i++)
155 public int getLength() {
159 public String getSubString(final int startInclusive, final int endExclusive) {
160 final char[] charArray = new char[endExclusive - startInclusive];
163 for (int i = startInclusive; i < endExclusive; i++) {
164 charArray[j] = chars.get(i);
167 return new String(charArray);
170 public boolean isEmpty() {
171 return chars.size() == 0;
175 public String toString() {
176 return getSubString(0, chars.size());
180 public static String[] getGroups(String s, String regexp){
181 Pattern pattern = Pattern.compile(regexp);
182 Matcher matcher = pattern.matcher(s);
185 String[] result = new String[matcher.groupCount()];
187 for (int i = 0; i< result.length; i++){
188 result[i] = matcher.group(i+1);