Use append/prepend instead of addSuffix/addPrefix
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / String2.java
index 0f01398..8d7273d 100755 (executable)
@@ -1,26 +1,43 @@
 /*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2020, 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 = new ArrayList<>();
 
+    public String2 clear(){
+        chars.clear();
+        return this;
+    }
+
     public String2(String value) {
-        addSuffix(value);
+        append(value);
+    }
+
+    public String2() {
+    }
+
+    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 addPrefix(final String prefix) {
+    public String2 prepend(final String prefix) {
         if (prefix == null)
             return this;
 
@@ -31,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;
 
@@ -41,15 +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.
      *
@@ -152,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;
+    }
 }