Class StringUtil



  • public final class StringUtil
    extends java.lang.Object
    Since:
    1.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String[] cloneArray​(java.lang.String[] inputArray)
      To make the clone copy of the given String array.
      static boolean containsMinus​(java.lang.String string)
      Checks if the given string contains minus (-).
      static boolean containsPlus​(java.lang.String string)
      Checks if the given string contains plus (+) char.
      static boolean containsSpace​(java.lang.String string)
      Checks if the given string contains space.
      static java.lang.String convertToSingleSpace​(java.lang.String input)
      Converts all continues multiple spaces to a single space so that the words will have a single space as separator.
      static int endIndexOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith)  
      static int endIndexOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith, int searchFromIndex)  
      static boolean endsWithColon​(java.lang.String string)
      Checks if the last char is a colon (:) char
      static boolean endsWithSpace​(java.lang.String string)
      Checks if the last char is a space char
      static boolean endsWithWhitespace​(java.lang.String value)  
      static java.lang.String[] getAllSubstrings​(java.lang.String inputString, java.lang.String startingWith, java.lang.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).
      static java.lang.String getFirstSubstring​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith)
      gets the first substring which starts and ends with the given values.
      static java.lang.String getFirstSubstring​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith, int searchFromIndex)
      gets the first substring which starts and ends with the given values.
      static boolean isEqual​(java.lang.String... strings)  
      static boolean isEqual​(java.lang.String string1, java.lang.String string2)  
      static java.lang.String[] split​(java.lang.String string, char delim)  
      static java.lang.String[] splitByColon​(java.lang.String string)
      Splits the given string by colon (:).
      static java.lang.String[] splitByComma​(java.lang.String string)
      Splits the given string by comma (,).
      static java.lang.String[] splitBySemicolon​(java.lang.String string)
      Splits the given string by semicolon (;).
      static java.lang.String[] splitBySpace​(java.lang.String string)
      Splits the given string by space.
      static int[][] startAndEndIndexesOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.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).
      static int[] startIndexAndEndIndexOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith)  
      static int[] startIndexAndEndIndexOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith, int searchFromIndex)  
      static int startIndexOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith)  
      static int startIndexOf​(java.lang.String inputString, java.lang.String startingWith, java.lang.String endingWith, int searchFromIndex)  
      static boolean startsWithSpace​(java.lang.String string)
      Checks if the first char is a space char
      static boolean startsWithWhitespace​(java.lang.String value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • convertToSingleSpace

        public static java.lang.String convertToSingleSpace​(java.lang.String input)
        Converts all continues multiple spaces to a single space so that the words will have a single space as separator.
        Parameters:
        input - the string in which all continues multiple spaces required to be converted to single space.
        Returns:
        the converted string having a single space in between words.
        Since:
        1.0.0
      • getFirstSubstring

        public static java.lang.String getFirstSubstring​(java.lang.String inputString,
                                                         java.lang.String startingWith,
                                                         java.lang.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 java.lang.String getFirstSubstring​(java.lang.String inputString,
                                                         java.lang.String startingWith,
                                                         java.lang.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 java.lang.String[] getAllSubstrings​(java.lang.String inputString,
                                                          java.lang.String startingWith,
                                                          java.lang.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​(java.lang.String inputString,
                                       java.lang.String startingWith,
                                       java.lang.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​(java.lang.String inputString,
                                     java.lang.String startingWith,
                                     java.lang.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​(java.lang.String inputString,
                                       java.lang.String startingWith,
                                       java.lang.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​(java.lang.String inputString,
                                     java.lang.String startingWith,
                                     java.lang.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​(java.lang.String inputString,
                                                    java.lang.String startingWith,
                                                    java.lang.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​(java.lang.String inputString,
                                                    java.lang.String startingWith,
                                                    java.lang.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​(java.lang.String inputString,
                                                   java.lang.String startingWith,
                                                   java.lang.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 java.lang.String[] cloneArray​(java.lang.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​(java.lang.String... strings)
        Returns:
        true if all strings are equal or null.
        Since:
        1.0.0
      • isEqual

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

        public static boolean startsWithWhitespace​(java.lang.String value)
        Parameters:
        value -
        Returns:
        true if the given value is starting with a white space
        Since:
        2.1.8
      • endsWithWhitespace

        public static boolean endsWithWhitespace​(java.lang.String value)
        Parameters:
        value -
        Returns:
        true if the given value is ending with a white space
        Since:
        2.1.8
      • split

        public static java.lang.String[] split​(java.lang.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
      • splitBySpace

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

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

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

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

        public static boolean endsWithSpace​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.String string)
        Checks if the given string contains space.
        Parameters:
        string -
        Returns:
        true if the given string contains space char.
        Since:
        2.1.15
      • containsMinus

        public static boolean containsMinus​(java.lang.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​(java.lang.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