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(String value) {
23 public String2 repeat(int count){
24 String value = toString();
26 for (int i = 1; i < count; i++) {
32 public String2 addPrefix(final String prefix) {
37 for (final char c : prefix.toCharArray())
43 public String2 addSuffix(final String suffix) {
47 for (final char c : suffix.toCharArray())
53 public String2 addSuffix(String separator, final String suffix) {
62 public String2 addSuffix(String s, int times) {
63 for (int i = 0; i < times; i++) addSuffix(s);
69 * Cut given amount of characters from the left of the string.
71 * @param cutAmount of characters to cut
72 * @return part that was cut.
74 public String2 trimPrefix(final int cutAmount) {
76 int actualCutAmount = cutAmount;
78 if (actualCutAmount > getLength())
79 actualCutAmount = getLength();
81 chars.subList(0, actualCutAmount).clear();
86 public String2 trimPrefixIfExists(String prefix) {
90 if (hasPrefix(prefix))
91 trimPrefix(prefix.length());
96 public String2 trimSuffixIfExists(String suffix) {
97 if (hasSuffix(suffix))
98 trimSuffix(suffix.length());
103 public String2 trimSuffix(int charsToTrim) {
105 if (charsToTrim > chars.size()) {
110 for (int i = 0; i < charsToTrim; i++)
111 chars.remove(chars.size() - 1);
116 public boolean hasSuffix(String suffix) {
117 return contains(suffix, getLength() - suffix.length());
120 public boolean hasPrefix(String prefix) {
121 return contains(prefix, 0);
124 public boolean contains(String fragment, int index) {
125 if (index + fragment.length() > chars.size())
128 for (int i = 0; i < fragment.length(); i++)
129 if (chars.get(index + i) != fragment.charAt(i))
135 public String2 enforceLength(final int targetLength) {
136 if (getLength() > targetLength)
137 chars.subList(targetLength, getLength()).clear();
138 else if (getLength() < targetLength) {
139 final int charactersToAdd = targetLength - getLength();
140 for (int i = 0; i < charactersToAdd; i++)
147 public int getLength() {
151 public String getSubString(final int startInclusive, final int endExclusive) {
152 final char[] charArray = new char[endExclusive - startInclusive];
155 for (int i = startInclusive; i < endExclusive; i++) {
156 charArray[j] = chars.get(i);
159 return new String(charArray);
162 public boolean isEmpty() {
163 return chars.size() == 0;
167 public String toString() {
168 return getSubString(0, chars.size());
172 public static String[] getGroups(String s, String regexp){
173 Pattern pattern = Pattern.compile(regexp);
174 Matcher matcher = pattern.matcher(s);
177 String[] result = new String[matcher.groupCount()];
179 for (int i = 0; i< result.length; i++){
180 result[i] = matcher.group(i+1);