X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2FString2.java;h=8d7273d8235bbfe0bbb28b294169fc22f6fa753f;hb=5cb739fecf46c9720fbdcb093364d55055d0d1f6;hp=2e9446a5fc3edf67c153d0e68a9f5a6055f9a51a;hpb=c8c84ff3c00094e9e87313753b659934f4d4ae72;p=svjatoslav_commons.git diff --git a/src/main/java/eu/svjatoslav/commons/string/String2.java b/src/main/java/eu/svjatoslav/commons/string/String2.java index 2e9446a..8d7273d 100755 --- a/src/main/java/eu/svjatoslav/commons/string/String2.java +++ b/src/main/java/eu/svjatoslav/commons/string/String2.java @@ -1,31 +1,43 @@ /* - * 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; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class String2 { - private final List chars; + private final List chars = new ArrayList<>(); + + public String2 clear(){ + chars.clear(); + return this; + } public String2(String value) { - if (value == null) - value = ""; + append(value); + } - chars = new ArrayList<>(value.length()); - for (final Character c : value.toCharArray()) - chars.add(c); + public String2() { } - public String2 addPrefix(final String prefix) { + public String2 repeat(int count){ + if (count > 0) { + String value = toString(); + + for (int i = 1; i < count; i++) + append(value); + } else { + clear(); + } + return this; + } + + public String2 prepend(final String prefix) { if (prefix == null) return this; @@ -36,7 +48,7 @@ public class String2 { return this; } - public String2 addSuffix(final String suffix) { + public String2 append(final String suffix) { if (suffix == null) return this; @@ -46,14 +58,21 @@ public class String2 { return this; } - public String2 addSuffix(String separator, final String suffix) { + public String2 appendWithSeparator(String separator, final String suffix) { if (!isEmpty()) - addSuffix(separator); + append(separator); - addSuffix(suffix); + append(suffix); return this; } + + public String2 append(String s, int times) { + for (int i = 0; i < times; i++) append(s); + return this; + } + + /** * Cut given amount of characters from the left of the string. * @@ -72,7 +91,7 @@ public class String2 { return this; } - public String2 trimPrefixIfExists(String prefix){ + public String2 trimPrefixIfExists(String prefix) { if (prefix == null) return this; @@ -82,7 +101,7 @@ public class String2 { return this; } - public String2 trimSuffixIfExists(String suffix){ + public String2 trimSuffixIfExists(String suffix) { if (hasSuffix(suffix)) trimSuffix(suffix.length()); @@ -91,13 +110,13 @@ public class String2 { public String2 trimSuffix(int charsToTrim) { - if (charsToTrim > chars.size()){ + if (charsToTrim > chars.size()) { chars.clear(); return this; } - for (int i = 0; i chars.size()) return false; @@ -156,4 +175,19 @@ public class String2 { public String toString() { return getSubString(0, chars.size()); } + + + public static String[] getGroups(String s, String regexp){ + Pattern pattern = Pattern.compile(regexp); + Matcher matcher = pattern.matcher(s); + + matcher.find(); + String[] result = new String[matcher.groupCount()]; + + for (int i = 0; i< result.length; i++){ + result[i] = matcher.group(i+1); + } + + return result; + } }