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 6b36b5b..07e3fdb 100755 (executable)
@@ -6,6 +6,8 @@ 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 {
 
@@ -15,6 +17,9 @@ public class String2 {
         addSuffix(value);
     }
 
+    public String2() {
+    }
+
     public String2 repeat(int count){
         String value = toString();
 
@@ -54,6 +59,12 @@ public class String2 {
         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.
      *
@@ -156,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;
+    }
 }