Class StringHelper


  • public final class StringHelper
    extends Object
    Methods for String manipulation and other useful methods about strings.
    • Method Detail

      • toJSON

        public static String toJSON​(Object obj)
                             throws com.fasterxml.jackson.core.JsonProcessingException
        Throws:
        com.fasterxml.jackson.core.JsonProcessingException
      • createTableName

        public static String createTableName​(String catalog,
                                             String schema,
                                             String name)
        Returns the full tablename with catalog.schema.tablename.
        Parameters:
        catalog -
        schema -
        name -
        Returns:
      • createTableName

        public static String createTableName​(SQLTable table)
        Returns the full tablename with catalog.schema.tablename.
        Parameters:
        table -
        Returns:
      • replaceVariables

        public static String replaceVariables​(String text,
                                              Object... objects)
        Replaces all variables within the text with the given objects in a loop and returns the result.
        For Example:
        Input: text="hallo {0} ich bin {1} {2}", objects="hans", "max", "mustermann"
        Output: hallo hans ich bin max mustermann
        Parameters:
        text -
        objects -
        Returns:
      • getAllAliases

        public static Map<String,​String> getAllAliases​(String sql)
        Finds out all aliases within the given sql and returns a map with:
        key = alias-name value = original name
        Parameters:
        sql -
        Returns:
      • getRegexForAliasFinding

        public static String getRegexForAliasFinding()
      • getOrDefault

        public static String getOrDefault​(String value,
                                          String defaultValue)
        Returns the value if this is not empty or null, otherwise it return the defaultValue.
        This method is only an alternative for chaining string-values together.
        Parameters:
        value -
        defaultValue -
        Returns:
      • getStatementsFromSQL

        public static List<String> getStatementsFromSQL​(String sql)
        Returns all statements, from the given SQL, in a List.
        Parameters:
        sql -
        Returns:
      • extractTableFromSQL

        public static SQLTable extractTableFromSQL​(String sql)
        Returns and extract the SQLTable from the given SQL.
        The table is always the table after the "FROM" keyword.
        If no table is found or the sql is empty, it will return null.
        Parameters:
        sql -
        Returns:
      • getByRegexGroup

        public static List<String> getByRegexGroup​(String str,
                                                   String regex)
        Returns all matched groups from the str by the regex-expression.
        Parameters:
        str -
        regex -
        Returns:
      • extractTableFromSQL

        public static SQLTable extractTableFromSQL​(String statement,
                                                   int startPos)
        Extracs the first SQLTable from the given sql-statement starting at the position startPos form the statement.
        Parameters:
        statement -
        startPos -
        Returns:
      • getStartsWith

        public static String getStartsWith​(String str,
                                           String... startsWiths)
        Gets the found startsWith from the array, to check if the str startWith one of these words.
        If nothing found it returns null.
        Parameters:
        str -
        startsWiths -
        Returns:
      • extractTableName

        public static String extractTableName​(String statement,
                                              String sql,
                                              int startPos)
        Gets and extract the tableName from the sql starting from the given startPos.
        Parameters:
        sql -
        startPos -
        Returns:
      • transformToLocalPos

        public static int transformToLocalPos​(int caretPos,
                                              String statement,
                                              String sql)
        Transforms the position of the statement in relation to the whole sql and its caretPosition.

        For example:
        Statement1: SELECT * FROM TABLE1;
        Statement2: SELECT *[|] FROM TABLE2

        The [|] represents the caret position then the caretPosition has a value of 30 but the transformed position will be 9
        So the transformed position will always be the local positon of the statement.
        Parameters:
        caretPos -
        statement -
        sql -
        Returns:
      • extractAllJoinedTableNames

        public static List<String> extractAllJoinedTableNames​(String statement)
        Gets and extracts all joined tableNames from the sql starting from the given startPos.
        Parameters:
        statement -
        Returns:
      • isEmpty

        public static boolean isEmpty​(String str)
        Checks if the string str is null or empty.
        Parameters:
        str -
        Returns:
      • isNotEmpty

        public static boolean isNotEmpty​(String str)
        Checks if the string str is not null and not empty.
        Parameters:
        str -
        Returns:
      • extractSingleStatement

        public static String extractSingleStatement​(String sql,
                                                    int startPos)
        Extracts any sql and return a single statement.
        Parameters:
        sql - = the whole sql which has to be extracted a single statement of it.
        startPos - = for example the caret-position in a text-editor.
        Returns:
      • indexOf

        public static int indexOf​(String str,
                                  String regex)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        regex - = Pattern to locate.
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • indexOf

        public static int indexOf​(String str,
                                  String regex,
                                  boolean toEndOfExpression)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        regex - = Pattern to locate.
        toEndOfExpression - = should the index return-value be the start or end of the expression
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • indexOf

        public static int indexOf​(String str,
                                  String regex,
                                  boolean toEndOfExpression,
                                  boolean substractExpressionLength)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        regex - = Pattern to locate.
        toEndOfExpression - = should the index return-value be the start or end of the expression
        substractExpressionLength - = should the length of the found group be substracted by the found positionIndex
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • indexOf

        public static int indexOf​(String str,
                                  int startPos,
                                  String regex,
                                  boolean toEndOfExpression,
                                  boolean substractExpressionLength)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        startPos - = Start-position
        regex - = Pattern to locate.
        toEndOfExpression - = should the index return-value be the start or end of the expression
        substractExpressionLength - = should the length of the found group be substracted by the found positionIndex
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • lastIndexOf

        public static int lastIndexOf​(String str,
                                      String regex)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        regex - = Pattern to locate.
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • lastIndexOf

        public static int lastIndexOf​(String str,
                                      String regex,
                                      boolean toEndOfExpression)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        regex - = Pattern to locate.
        toEndOfExpression - = should the index return-value be the start or end of the expression
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • lastIndexOf

        public static int lastIndexOf​(String str,
                                      String regex,
                                      boolean toEndOfExpression,
                                      boolean substractExpressionLength)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        regex - = Pattern to locate.
        toEndOfExpression - = should the index return-value be the start or end of the expression
        substractExpressionLength - = should the length of the found group be substracted by the found positionIndex
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • lastIndexOf

        public static int lastIndexOf​(String str,
                                      int startPos,
                                      String regex,
                                      boolean toEndOfExpression,
                                      boolean substractExpressionLength)
        IndexOf-variant that uses regular expressions for searching.
        Parameters:
        str - = String in which to search for the pattern.
        startPos - = startPosition
        regex - = Pattern to locate.
        toEndOfExpression - = should the index return-value be the start or end of the expression
        substractExpressionLength - = should the length of the found group be substracted by the found positionIndex
        Returns:
        The index of the requested pattern, if found; (-1) otherwise.
      • getLimitValueFromSQL

        public static String getLimitValueFromSQL​(String sql)
        Returns the amount-value from an "limit" within the sql.
        For Example select limit 500 * from table,
        then it will return "500".
        if nothing found it will return null.
        Parameters:
        sql -
        Returns:
      • endsWith

        public static boolean endsWith​(String string,
                                       String... suffixes)
        Checks if the string ends with one of the given suffixes.
        Parameters:
        string -
        suffixes -
        Returns:
      • startsWith

        public static boolean startsWith​(String string,
                                         String... prefixes)
        Checks if the string starts with one of the given suffixes.
        Parameters:
        string -
        prefixes -
        Returns:
      • getExecutionPath

        public static String getExecutionPath()
        Returns the execution-path of the program, depending on the OS.
        Returns:
      • prepareSQL

        public static String prepareSQL​(String sql)
        Prepares the given SQL. That means to remove all singleline/multiline comments from the SQL.
        Parameters:
        sql - = SQL to prepare
      • replaceWhitespaces

        public static String replaceWhitespaces​(String str)
        Replaces all whitespaces withing the string str.
        Parameters:
        str -
        Returns:
      • replaceRegion

        public static String replaceRegion​(String text,
                                           int start,
                                           int till,
                                           String replacement)
        Replaces the region between start and till with the replacement.
        Parameters:
        text -
        start -
        till -
        replacement -
        Returns:
      • looksLikeUTF8

        public static boolean looksLikeUTF8​(byte[] pText)
        Checks if the given byte-array is like a utf8 string
        Parameters:
        pText -
        Returns:
      • escapeSQL

        public static String escapeSQL​(String sql)
        Escapes all comments and quotes inside the given sql.
        Parameters:
        sql -
        Returns: