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);
125 * Check if the string has a suffix.
126 * @param suffix to check
127 * @return true if the string has the suffix
129 public boolean hasSuffix(String suffix) {
130 return contains(suffix, getLength() - suffix.length());
134 * Check if the string has a prefix.
135 * @param prefix to check
136 * @return true if the string has the prefix
138 public boolean hasPrefix(String prefix) {
139 return contains(prefix, 0);
142 public boolean contains(String fragment, int index) {
143 if (index + fragment.length() > chars.size())
146 for (int i = 0; i < fragment.length(); i++)
147 if (chars.get(index + i) != fragment.charAt(i))
153 public String2 enforceLength(final int targetLength) {
154 if (getLength() > targetLength)
155 chars.subList(targetLength, getLength()).clear();
156 else if (getLength() < targetLength) {
157 final int charactersToAdd = targetLength - getLength();
158 for (int i = 0; i < charactersToAdd; i++)
165 public int getLength() {
169 public String getSubString(final int startInclusive, final int endExclusive) {
170 final char[] charArray = new char[endExclusive - startInclusive];
173 for (int i = startInclusive; i < endExclusive; i++) {
174 charArray[j] = chars.get(i);
177 return new String(charArray);
180 public boolean isEmpty() {
181 return chars.size() == 0;
185 public String toString() {
186 return getSubString(0, chars.size());
190 public static String[] getGroups(String s, String regexp){
191 Pattern pattern = Pattern.compile(regexp);
192 Matcher matcher = pattern.matcher(s);
195 String[] result = new String[matcher.groupCount()];
197 for (int i = 0; i< result.length; i++){
198 result[i] = matcher.group(i+1);