Helper function to split string into groups based on regexp. Possibility to retrieve...
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / String2.java
index 504c41d..07e3fdb 100755 (executable)
@@ -1,28 +1,38 @@
 /*
- * 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<Character> chars;
+    private final List<Character> chars = new ArrayList<>();
 
-    public String2(final String value) {
-        chars = new ArrayList<>(value.length());
-        for (final Character c : value.toCharArray())
-            chars.add(c);
+    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);
@@ -31,12 +41,30 @@ public class String2 {
     }
 
     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.
      *
@@ -55,14 +83,17 @@ public class String2 {
         return this;
     }
 
-    public String2 trimPrefixIfExists(String prefix){
+    public String2 trimPrefixIfExists(String prefix) {
+        if (prefix == null)
+            return this;
+
         if (hasPrefix(prefix))
             trimPrefix(prefix.length());
 
         return this;
     }
 
-    public String2 trimSuffixIfExists(String suffix){
+    public String2 trimSuffixIfExists(String suffix) {
         if (hasSuffix(suffix))
             trimSuffix(suffix.length());
 
@@ -71,13 +102,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<charsToTrim; i++)
-            chars.remove(chars.size()-1);
+        for (int i = 0; i < charsToTrim; i++)
+            chars.remove(chars.size() - 1);
 
         return this;
     }
@@ -86,11 +117,11 @@ public class String2 {
         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;
 
@@ -136,4 +167,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;
+    }
 }