/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2017, 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.
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
*/
-
package eu.svjatoslav.commons.string;
import java.util.ArrayList;
public class String2 {
- private final List<Character> chars;
+ private final List<Character> chars = new ArrayList<>();
public String2(String value) {
- if (value == null)
- value = "";
-
- chars = new ArrayList<>(value.length());
- for (final Character c : value.toCharArray())
- chars.add(c);
+ addSuffix(value);
}
public String2 addPrefix(final String prefix) {
return this;
}
+
/**
* Cut given amount of characters from the left of the string.
*
return this;
}
- public String2 trimPrefixIfExists(String prefix){
+ public String2 trimPrefixIfExists(String prefix) {
if (prefix == null)
return this;
return this;
}
- public String2 trimSuffixIfExists(String suffix){
+ public String2 trimSuffixIfExists(String suffix) {
if (hasSuffix(suffix))
trimSuffix(suffix.length());
public String2 trimSuffix(int charsToTrim) {
- if (charsToTrim > chars.size()){
+ if (charsToTrim > chars.size()) {
chars.clear();
return this;
}
- for (int i = 0; i<charsToTrim; i++)
- chars.remove(chars.size()-1);
+ for (int i = 0; i < charsToTrim; i++)
+ chars.remove(chars.size() - 1);
return this;
}
return contains(suffix, getLength() - suffix.length());
}
- public boolean hasPrefix(String prefix){
+ public boolean hasPrefix(String prefix) {
return contains(prefix, 0);
}
- public boolean contains(String fragment, int index){
+ public boolean contains(String fragment, int index) {
if (index + fragment.length() > chars.size())
return false;