Class StringUtil

java.lang.Object
com.webfirmframework.wffweb.util.StringUtil

public final class StringUtil extends Object
Since:
1.0.0
  • Method Details

    • convertToSingleSpace

      public static String convertToSingleSpace(String input)
      Converts all continues multiple spaces and new line/lines followed by space/spaces to a single space.
      Parameters:
      input - the string to convert
      Returns:
      the converted string having a single space in between characters.
      Since:
      1.0.0
    • convertSpacesToSingleSpace

      public static String convertSpacesToSingleSpace(String input)
      Converts all continues multiple spaces to a single space.
      Parameters:
      input - the string to convert
      Returns:
      the converted string having a single space in between characters.
      Since:
      3.0.15 it is unicode aware
    • convertWhitespacesToSingleSpace

      public static String convertWhitespacesToSingleSpace(String input)
      Converts all continues multiple whitespaces to a single space. Please know that new line and tab are also considered as whitespace.
      Parameters:
      input - the string to convert
      Returns:
      the converted string having a single space in between characters.
      Since:
      3.0.15 it is unicode aware.
    • removeSpaces

      public static String removeSpaces(String input)
      Removes all spaces from the string.
      Parameters:
      input -
      Returns:
      the string without spaces.
      Since:
      3.0.15 it is unicode aware.
    • removeWhitespaces

      public static String removeWhitespaces(String input)
      Removes all whitespaces from the string. New line and tab are also whiltespace.
      Parameters:
      input -
      Returns:
      the string without spaces.
      Since:
      3.0.15 it is unicode aware.
    • getFirstSubstring

      public static String getFirstSubstring(String inputString, String startingWith, String endingWith)
      gets the first substring which starts and ends with the given values.
      Parameters:
      inputString - the string from which the substring will be extracted.
      startingWith - to match the starting substring
      endingWith - to match the ending substring
      Returns:
      the substring which starts and ends with the given startingWith and endingWith values. Or returns null if it doesn't contain.
      Since:
      1.0.0
    • getFirstSubstring

      public static String getFirstSubstring(String inputString, String startingWith, String endingWith, int searchFromIndex)
      gets the first substring which starts and ends with the given values.
      Parameters:
      inputString - the string from which the substring will be extracted.
      startingWith - to match the starting substring
      endingWith - to match the ending substring
      searchFromIndex - the starting index from where the substring should be searched.
      Returns:
      the substring which starts and ends with the given startingWith and endingWith values. Or returns null if it doesn't contain.
      Since:
      1.0.0
    • getAllSubstrings

      public static String[] getAllSubstrings(String inputString, String startingWith, String endingWith)
      gets all substrings as an array which starts and ends with the given values.
      Note:- it will never return null instead it will return an empty array (having length zero).
      
       final String[] allSubstrings = getAllSubstrings("abcd", "ab", "cd");
       for (String each : allSubstrings) {
           System.out.println(each);
       }
       the output will be : abcd
       
      Parameters:
      inputString - the string from which the substrings will be extracted.
      startingWith - to match the starting substring
      endingWith - to match the ending substring
      Returns:
      the array of substrings which contains strings starting and ending with the given startingWith and endingWith values. Or returns an empty array (i.e an array having length zero) if it doesn't contain.
      Since:
      1.0.0
    • startIndexOf

      public static int startIndexOf(String inputString, String startingWith, String endingWith)
      Parameters:
      inputString - the string from the index should be found.
      startingWith - the substring to check whether it comes in the given inputString before endingWith substring.
      endingWith - the substring to check whether it comes in the given inputString after startingWith substring.
      Returns:
      the index of startingWith substring if the given inputString contains a substring starting with startingWith string and ending with endingWith string, otherwise returns -1.
      Since:
      1.0.0
    • endIndexOf

      public static int endIndexOf(String inputString, String startingWith, String endingWith)
      Parameters:
      inputString - the string from the index should be found.
      startingWith - the substring to check whether it comes in the given inputString before endingWith substring.
      endingWith - the substring to check whether it comes in the given inputString after startingWith substring.
      Returns:
      the index of endingWith substring if the given inputString contains a substring starting with startingWith string and ending with endingWith string, otherwise returns -1.
      Since:
      1.0.0
    • startIndexOf

      public static int startIndexOf(String inputString, String startingWith, String endingWith, int searchFromIndex)
      Parameters:
      inputString - the string from the index should be found.
      startingWith - the substring to check whether it comes in the given inputString before endingWith substring.
      endingWith - the substring to check whether it comes in the given inputString after startingWith substring.
      searchFromIndex - the starting index from where the substring should be searched.
      Returns:
      the index of startingWith substring if the given inputString contains a substring starting with startingWith string and ending with endingWith string, otherwise returns -1.
      Since:
      1.0.0
    • endIndexOf

      public static int endIndexOf(String inputString, String startingWith, String endingWith, int searchFromIndex)
      Parameters:
      inputString - the string from the index should be found.
      startingWith - the substring to check whether it comes in the given inputString before endingWith substring.
      endingWith - the substring to check whether it comes in the given inputString after startingWith substring.
      searchFromIndex - the starting index from where the substring should be searched.
      Returns:
      the index of endingWith substring if the given inputString contains a substring starting with startingWith string and ending with endingWith string, otherwise returns -1.
      Since:
      1.0.0
    • startIndexAndEndIndexOf

      public static int[] startIndexAndEndIndexOf(String inputString, String startingWith, String endingWith)
      Parameters:
      inputString - the string from the index should be found.
      startingWith - the substring to check whether it comes in the given inputString before endingWith substring.
      endingWith - the substring to check whether it comes in the given inputString after startingWith substring.
      Returns:
      an array containing the index of startingWith substring if the given inputString contains a substring starting with startingWith string and ending with endingWith string, otherwise returns an empty array.
      Since:
      1.0.0
    • startIndexAndEndIndexOf

      public static int[] startIndexAndEndIndexOf(String inputString, String startingWith, String endingWith, int searchFromIndex)
      Parameters:
      inputString - the string from the index should be found.
      startingWith - the substring to check whether it comes in the given inputString before endingWith substring.
      endingWith - the substring to check whether it comes in the given inputString after startingWith substring.
      searchFromIndex - the starting index from where the substring should be searched.
      Returns:
      an array containing the index of startingWith substring if the given inputString contains a substring starting with startingWith string and ending with endingWith string, otherwise returns an empty array.
      Since:
      1.0.0
    • startAndEndIndexesOf

      public static int[][] startAndEndIndexesOf(String inputString, String startingWith, String endingWith)
      gets all start and end indexes as an array of array which start and end with the given values.
      Note:- it will never return null instead it will return an empty array (having length zero).
      
               final int[][] startAndEndIndexesOf = startAndEndIndexesOf(
                       "121 131 141 151 161 171 181 191 101", "1", "1");
      
               for (final int[] startIndexAndEndIndex : startAndEndIndexesOf) {
                   System.out.println(startIndexAndEndIndex[0] + " - "
                           + startIndexAndEndIndex[1]);
               }
      
       the output will be :
       
       0 - 3
       4 - 7
       8 - 11
       12 - 15
       16 - 19
       20 - 23
       24 - 27
       28 - 31
       32 - 35
       
      
               int[] startIndexAndEndIndex = startAndEndIndexesOf[0]; // 1, 2 etc..
               System.out.println(startIndexAndEndIndex[0] + " - "
                       + startIndexAndEndIndex[1]);
      
       the output will be : 0 - 3
       
      Parameters:
      inputString - the string from which the substrings will be extracted.
      startingWith - to match the starting substring
      endingWith - to match the ending substring
      Returns:
      the array of substrings which contains strings starting and ending with the given startingWith and endingWith values. Or returns an empty array (i.e an array having length zero) if it doesn't contain.
      Since:
      1.0.0
    • cloneArray

      public static String[] cloneArray(String[] inputArray)
      To make the clone copy of the given String array. This method is faster than the clone method of String array.
      Parameters:
      inputArray -
      Returns:
      the cloned array of the given string array.
      Since:
      1.0.0
    • isEqual

      public static boolean isEqual(String... strings)
      Returns:
      true if all strings are equal or null.
      Since:
      1.0.0
    • isEqual

      public static boolean isEqual(String string1, String string2)
      Returns:
      true if two strings are equal or null.
      Since:
      1.0.0
    • startsWithWhitespace

      public static boolean startsWithWhitespace(String value)
      Parameters:
      value -
      Returns:
      true if the given value is starting with a white space
      Since:
      2.1.8 initial implementation., 3.0.15 unicode aware.
    • endsWithWhitespace

      public static boolean endsWithWhitespace(String value)
      Parameters:
      value -
      Returns:
      true if the given value is ending with a white space
      Since:
      2.1.8 initial implementation., 3.0.15 unicode aware.
    • split

      public static String[] split(String string, char delim)
      Parameters:
      string - the string to split.
      delim - to by which the given string to be split.
      Returns:
      the array of strings split by the given char.
      Since:
      3.0.0 public, 3.0.15 unicode aware.
    • split

      public static String[] split(String string, int delim)
      Parameters:
      string - the string to split.
      delim - to by which the given string to be split. It is the code point value of char.
      Returns:
      the array of strings split by the given char.
      Since:
      3.0.15
    • splitBySpace

      public static String[] splitBySpace(String string)
      Splits the given string by space.
      Parameters:
      string -
      Returns:
      the array of strings
      Since:
      2.1.15
    • splitByComma

      public static String[] splitByComma(String string)
      Splits the given string by comma (,).
      Parameters:
      string -
      Returns:
      the array of strings
      Since:
      2.1.15
    • splitBySemicolon

      public static String[] splitBySemicolon(String string)
      Splits the given string by semicolon (;).
      Parameters:
      string -
      Returns:
      the array of strings
      Since:
      2.1.15
    • splitByColon

      public static String[] splitByColon(String string)
      Splits the given string by colon (:).
      Parameters:
      string -
      Returns:
      the array of strings
      Since:
      2.1.15
    • endsWithSpace

      public static boolean endsWithSpace(String string)
      Checks if the last char is a space char
      Parameters:
      string -
      Returns:
      true if the last character is a space char
      Since:
      2.1.15
    • endsWithColon

      public static boolean endsWithColon(String string)
      Checks if the last char is a colon (:) char
      Parameters:
      string -
      Returns:
      true if the last character is a colon (:) char
      Since:
      2.1.15
    • startsWithSpace

      public static boolean startsWithSpace(String string)
      Checks if the first char is a space char
      Parameters:
      string -
      Returns:
      true if the first character is a space char
      Since:
      2.1.15
    • containsSpace

      public static boolean containsSpace(String string)
      Checks if the given string contains space.
      Parameters:
      string -
      Returns:
      true if the given string contains space char.
      Since:
      2.1.15
    • containsWhitespace

      public static boolean containsWhitespace(String string)
      Checks if the given string contains whitespace.
      Parameters:
      string -
      Returns:
      true if the given string contains space char.
      Since:
      3.0.1 initial implementation., 3.0.15 it is unicode aware.
    • containsMinus

      public static boolean containsMinus(String string)
      Checks if the given string contains minus (-).
      Parameters:
      string -
      Returns:
      true if the given string contains minus (-) char.
      Since:
      2.1.15
    • containsPlus

      public static boolean containsPlus(String string)
      Checks if the given string contains plus (+) char.
      Parameters:
      string -
      Returns:
      true if the given string contains plus (+) char.
      Since:
      2.1.15
    • join

      public static String join(char delimiter, char prefix, char suffix, String... items)
      Eg:
       String join = StringUtil.join(',', ':', ';', "one", "two", "three", "four");
       prints :one,two,three,four;
       
      This method must be faster than Java 8's StringJoiner.
      Parameters:
      delimiter -
      prefix -
      suffix -
      items -
      Returns:
      String
      Since:
      3.0.1
    • join

      public static String join(char delimiter, String... items)
      Eg:
       String join = StringUtil.join(',', "one", "two", "three", "four");
       prints one,two,three,four
       
      This method must be faster than Java 8's String.join method.
      Parameters:
      delimiter -
      items -
      Returns:
      String
      Since:
      3.0.1
    • join

      public static String join(char delimiter, Collection<String> items)
      Eg:
       String join = StringUtil.join(',', "one", "two", "three", "four");
       prints one,two,three,four
       
      This method must be faster than Java 8's String.join method.
      Parameters:
      delimiter -
      items -
      Returns:
      String
      Since:
      3.0.1
    • isBlank

      public static boolean isBlank(String s)
      Parameters:
      s - String to check
      Returns:
      true if the given string doesn't contain any char other than whitespace.
      Since:
      3.0.1 initial implementation., 3.0.15 it is unicode aware.
    • strip

      public static String strip(String s)
      Removes the trailing and leading whitespaces
      Parameters:
      s -
      Returns:
      the striped string
      Since:
      3.0.1 initial implementation., 3.0.15 it is unicode aware.
    • replace

      public static String replace(String s, String it, String with)
      Parameters:
      s - the string for processing.
      it - the string for matching.
      with - the matching to be replaced with this value.
      Returns:
      the processed string
      Since:
      3.0.15
    • replace

      public static String replace(String s, int[] codePointsSequence, String with)
      Parameters:
      s - the string for processing.
      codePointsSequence - the array of code points for matching in its sequential order.
      with - the matching to be replaced with this value.
      Returns:
      the processed string
      Since:
      3.0.15
    • splitByAny

      public static String[] splitByAny(String string, int[] delims)
      Parameters:
      string - the string to split.
      delims - to by which the given string to be split. It is the code point values of chars.
      Returns:
      the array of strings split by the given charcodes.
      Since:
      3.0.15