Possibility to repeat string.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / String2.java
index b6083ed..6b36b5b 100755 (executable)
@@ -1,12 +1,7 @@
 /*
- * 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;
@@ -20,6 +15,15 @@ public class String2 {
         addSuffix(value);
     }
 
+    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;
@@ -49,6 +53,7 @@ public class String2 {
 
         return this;
     }
+
     /**
      * Cut given amount of characters from the left of the string.
      *
@@ -67,7 +72,7 @@ public class String2 {
         return this;
     }
 
-    public String2 trimPrefixIfExists(String prefix){
+    public String2 trimPrefixIfExists(String prefix) {
         if (prefix == null)
             return this;
 
@@ -77,7 +82,7 @@ public class String2 {
         return this;
     }
 
-    public String2 trimSuffixIfExists(String suffix){
+    public String2 trimSuffixIfExists(String suffix) {
         if (hasSuffix(suffix))
             trimSuffix(suffix.length());
 
@@ -86,13 +91,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;
     }
@@ -101,11 +106,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;