X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=svjatoslav_commons.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;h=07e3fdb83ccc81d366ab0734121fe475c85adb22;hp=b19e99a74e997c13e9138c4d498b1470e092fea7;hb=b1ffc7025cc976821987469570f07a7298ea16c9;hpb=2ee43de10e5311ba64e7fc855e253d02326e254c diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index b19e99a..07e3fdb 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -1,91 +1,185 @@ /* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, 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.Vector; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class String2 { - private final Vector chars = new Vector(); + private final List chars = new ArrayList<>(); + + public String2(String value) { + addSuffix(value); + } + + public String2() { + } + + public String2 repeat(int count){ + String value = toString(); + + for (int i = 1; i < count; i++) { + addSuffix(value); + } + return this; + } + + public String2 addPrefix(final String prefix) { + if (prefix == null) + return this; + + int i = 0; + for (final char c : prefix.toCharArray()) + chars.add(i++, c); + + return this; + } + + public String2 addSuffix(final String suffix) { + if (suffix == null) + return this; + + for (final char c : suffix.toCharArray()) + chars.add(c); + + return this; + } + + public String2 addSuffix(String separator, final String suffix) { + if (!isEmpty()) + addSuffix(separator); + + addSuffix(suffix); + + return this; + } + + public String2 addSuffix(String s, int times) { + for (int i = 0; i < times; i++) addSuffix(s); + return this; + } + + + /** + * Cut given amount of characters from the left of the string. + * + * @param cutAmount of characters to cut + * @return part that was cut. + */ + public String2 trimPrefix(final int cutAmount) { + + int actualCutAmount = cutAmount; + + if (actualCutAmount > getLength()) + actualCutAmount = getLength(); + + chars.subList(0, actualCutAmount).clear(); + + return this; + } + + public String2 trimPrefixIfExists(String prefix) { + if (prefix == null) + return this; + + if (hasPrefix(prefix)) + trimPrefix(prefix.length()); + + return this; + } + + public String2 trimSuffixIfExists(String suffix) { + if (hasSuffix(suffix)) + trimSuffix(suffix.length()); + + return this; + } + + public String2 trimSuffix(int charsToTrim) { + + if (charsToTrim > chars.size()) { + chars.clear(); + return this; + } + + for (int i = 0; i < charsToTrim; i++) + chars.remove(chars.size() - 1); - public String2(final String value) { - for (final Character c : value.toCharArray()) - chars.add(c); - } + return this; + } - public String2 addPrefix(final String prefix) { - int j = 0; - for (final char c : prefix.toCharArray()) - chars.insertElementAt(c, j++); + public boolean hasSuffix(String suffix) { + return contains(suffix, getLength() - suffix.length()); + } - return this; - } + public boolean hasPrefix(String prefix) { + return contains(prefix, 0); + } - public String2 addSuffix(final String suffix) { - for (final char c : suffix.toCharArray()) - chars.add(c); + public boolean contains(String fragment, int index) { + if (index + fragment.length() > chars.size()) + return false; - return this; - } + for (int i = 0; i < fragment.length(); i++) + if (chars.get(index + i) != fragment.charAt(i)) + return false; - /** - * Cut given amount of characters from the left of the string. Return cutted - * part. - */ - public String cutLeft(final int cutAmount) { + return true; + } - int actualCutAmount = cutAmount; + public String2 enforceLength(final int targetLength) { + if (getLength() > targetLength) + chars.subList(targetLength, getLength()).clear(); + else if (getLength() < targetLength) { + final int charactersToAdd = targetLength - getLength(); + for (int i = 0; i < charactersToAdd; i++) + chars.add(' '); + } - if (actualCutAmount > getLength()) - actualCutAmount = getLength(); + return this; + } - final String result = getSubString(0, actualCutAmount); + public int getLength() { + return chars.size(); + } - chars.subList(0, actualCutAmount).clear(); + public String getSubString(final int startInclusive, final int endExclusive) { + final char[] charArray = new char[endExclusive - startInclusive]; - return result; - } + int j = 0; + for (int i = startInclusive; i < endExclusive; i++) { + charArray[j] = chars.get(i); + j++; + } + return new String(charArray); + } - public String2 enforceLength(final int targetLength) { - if (getLength() > targetLength) - chars.subList(targetLength, getLength()).clear(); - else if (getLength() < targetLength) { - final int charactersToAdd = targetLength - getLength(); - for (int i = 0; i < charactersToAdd; i++) - chars.add(' '); - } + public boolean isEmpty() { + return chars.size() == 0; + } - return this; - } + @Override + public String toString() { + return getSubString(0, chars.size()); + } - public int getLength() { - return chars.size(); - } - public String getSubString(final int startInclusive, final int endExclusive) { - final char[] charArray = new char[endExclusive - startInclusive]; + public static String[] getGroups(String s, String regexp){ + Pattern pattern = Pattern.compile(regexp); + Matcher matcher = pattern.matcher(s); - int j = 0; - for (int i = startInclusive; i < endExclusive; i++) { - charArray[j] = chars.get(i); - j++; - } - return new String(charArray); - } + matcher.find(); + String[] result = new String[matcher.groupCount()]; - public boolean isEmpty() { - return chars.size() == 0; - } + for (int i = 0; i< result.length; i++){ + result[i] = matcher.group(i+1); + } - @Override - public String toString() { - return getSubString(0, chars.size()); - } + return result; + } }