Class StringUtil



  • public 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 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 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 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 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.
        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
      • 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