Class StringFunctions


  • public class StringFunctions
    extends Object
    Inbuilt String Transformation Functions The functions can be used as UDFs in Query when added in the FunctionRegistry.
    • Method Detail

      • reverse

        public static String reverse​(String input)
        Parameters:
        input -
        Returns:
        reversed input in from end to start
        See Also:
        StringUtils.reverse(String)
      • lower

        public static String lower​(String input)
        Parameters:
        input -
        Returns:
        string in lower case format
      • substr

        public static String substr​(String input,
                                    int beginIndex)
        Parameters:
        input - Parent string
        beginIndex - index from which substring should be created
        Returns:
        substring from beginIndex to end of the parent string
        See Also:
        String.substring(int)
      • substr

        public static String substr​(String input,
                                    int beginIndex,
                                    int endIndex)
        Returns the substring of the main string from beginIndex to endIndex. If endIndex is -1 returns the substring from begingIndex to end of the string.
        Parameters:
        input - Parent string
        beginIndex - index from which substring should be created
        endIndex - index at which substring should be terminated
        Returns:
        substring from beginIndex to endIndex
        See Also:
        String.substring(int, int)
      • concat

        public static String concat​(String input1,
                                    String input2,
                                    String seperator)
        Join two input string with seperator in between
        Parameters:
        input1 -
        input2 -
        seperator -
        Returns:
        The two input strings joined by the seperator
      • trim

        public static String trim​(String input)
        Parameters:
        input -
        Returns:
        trim spaces from both ends of the string
        See Also:
        String.trim()
      • trim

        public static String trim​(String end,
                                  String characters,
                                  String value)
        Standard SQL trim function.
        Parameters:
        end - BOTH|LEADING|TRAILING
        characters - characters to be trimmed off
        value - value to trim
        Returns:
        trim the characters from both/leading/trailing end of the string
      • ltrim

        public static String ltrim​(String input)
        Parameters:
        input -
        Returns:
        trim spaces from left side of the string
      • rtrim

        public static String rtrim​(String input)
        Parameters:
        input -
        Returns:
        trim spaces from right side of the string
      • leftSubStr

        public static String leftSubStr​(String input,
                                        int length)
        Parameters:
        input -
        Returns:
        get substring starting from the first index and extending upto specified length.
        See Also:
        StringUtils.left(String, int)
      • rightSubStr

        public static String rightSubStr​(String input,
                                         int length)
        Parameters:
        input -
        Returns:
        get substring ending at the last index with specified length
        See Also:
        StringUtils.right(String, int)
      • regexpExtract

        public static String regexpExtract​(String value,
                                           String regexp)
        Parameters:
        value -
        regexp -
        Returns:
        the matched result.
      • regexpExtract

        public static String regexpExtract​(String value,
                                           String regexp,
                                           int group)
        Parameters:
        value -
        regexp -
        group -
        Returns:
        the matched result.
      • regexpExtract

        public static String regexpExtract​(String value,
                                           String regexp,
                                           int group,
                                           String defaultValue)
        Regular expression that extract first matched substring.
        Parameters:
        value - input value
        regexp - regular expression
        group - the group number within the regular expression to extract.
        defaultValue - the default value if no match found
        Returns:
        the matched result
      • length

        public static int length​(String input)
        Parameters:
        input -
        Returns:
        length of the string
        See Also:
        String.length()
      • strpos

        public static int strpos​(String input,
                                 String find,
                                 int instance)
        Parameters:
        input -
        find - substring to find
        instance - Integer denoting the instance no.
        Returns:
        start index of the Nth instance of substring in main string
        See Also:
        Return the Nth occurence of a substring within the String
      • strpos

        public static int strpos​(String input,
                                 String find)
        Parameters:
        input -
        find - substring to find
        Returns:
        start index of the 1st instance of substring in main string
        See Also:
        Return the 1st occurence of a substring within the String
      • strrpos

        public static int strrpos​(String input,
                                  String find)
        Parameters:
        input -
        find - substring to find
        Returns:
        start index of the last instance of substring in main string
        See Also:
        Return the last occurence of a substring within the String
      • strrpos

        public static int strrpos​(String input,
                                  String find,
                                  int instance)
        Parameters:
        input -
        find - substring to find
        instance - Integer denoting the instance no.
        Returns:
        start index of the Nth instance of substring in main string starting from the end of the string.
        See Also:
        Return the Nth occurence of a substring in string starting from the end of the string.
      • startsWith

        public static boolean startsWith​(String input,
                                         String prefix)
        Parameters:
        input -
        prefix - substring to check if it is the prefix
        Returns:
        true if string starts with prefix, false o.w.
        See Also:
        StringUtils.startsWith(CharSequence, CharSequence)
      • endsWith

        public static boolean endsWith​(String input,
                                       String suffix)
        Parameters:
        input -
        suffix - substring to check if it is the prefix
        Returns:
        true if string ends with prefix, false o.w.
        See Also:
        StringUtils.endsWith(CharSequence, CharSequence)
      • rpad

        public static String rpad​(String input,
                                  int size,
                                  String pad)
        Parameters:
        input -
        size - final size of the string
        pad - pad string to be used
        Returns:
        string padded from the right side with pad to reach final size
        See Also:
        StringUtils.rightPad(String, int, char)
      • lpad

        public static String lpad​(String input,
                                  int size,
                                  String pad)
        Parameters:
        input -
        size - final size of the string
        pad - pad string to be used
        Returns:
        string padded from the left side with pad to reach final size
        See Also:
        StringUtils.leftPad(String, int, char)
      • codepoint

        public static int codepoint​(String input)
        Parameters:
        input -
        Returns:
        the Unicode codepoint of the first character of the string
        See Also:
        String.codePointAt(int)
      • chr

        public static String chr​(int codepoint)
        Parameters:
        codepoint -
        Returns:
        the character corresponding to the Unicode codepoint
        See Also:
        Character.toChars(int)
      • toUtf8

        public static byte[] toUtf8​(String input)
        Parameters:
        input -
        Returns:
        bytes
      • fromUtf8

        public static String fromUtf8​(byte[] input)
        Parameters:
        input - bytes
        Returns:
        UTF8 encoded string
      • toAscii

        public static byte[] toAscii​(String input)
        Parameters:
        input -
        Returns:
        bytes
      • split

        public static String[] split​(String input,
                                     String delimiter)
        Parameters:
        input -
        delimiter -
        Returns:
        splits string on specified delimiter and returns an array.
        See Also:
        StringUtils.split(String, String)
      • split

        public static String[] split​(String input,
                                     String delimiter,
                                     int limit)
        Parameters:
        input -
        delimiter -
        limit -
        Returns:
        splits string on specified delimiter limiting the number of results till the specified limit
      • splitPart

        public static String splitPart​(String input,
                                       String delimiter,
                                       int index)
        Parameters:
        input -
        delimiter -
        index -
        Returns:
        splits string on specified delimiter and returns String at specified index from the split.
      • repeat

        public static String repeat​(String input,
                                    int times)
        Parameters:
        input -
        times -
        Returns:
        concatenate the string to itself specified number of times
        See Also:
        StringUtils.repeat(char, int)
      • repeat

        public static String repeat​(String input,
                                    String sep,
                                    int times)
        Parameters:
        input -
        times -
        Returns:
        concatenate the string to itself specified number of times with specified seperator
        See Also:
        StringUtils.repeat(String, String, int)
      • remove

        public static String remove​(String input,
                                    String search)
        Parameters:
        input -
        search -
        Returns:
        removes all instances of search from string
        See Also:
        StringUtils.remove(String, String)
      • hammingDistance

        public static int hammingDistance​(String input1,
                                          String input2)
        Parameters:
        input1 -
        input2 -
        Returns:
        returns the Hamming distance of input1 and input2, note that the two strings must have the same length.
      • contains

        public static boolean contains​(String input,
                                       String substring)
        Parameters:
        input -
        substring -
        Returns:
        returns true if substring present in main string else false.
        See Also:
        String.contains(CharSequence)
      • strcmp

        public static int strcmp​(String input1,
                                 String input2)
        Compare input strings lexicographically.
        Returns:
        the value 0 if the first string argument is equal to second string; a value less than 0 if first string argument is lexicographically less than the second string argument; and a value greater than 0 if the first string argument is lexicographically greater than the second string argument.
      • toBase64

        public static String toBase64​(byte[] input)
        Parameters:
        input - binary data
        Returns:
        Base64 encoded String
      • fromBase64

        public static byte[] fromBase64​(String input)
        Parameters:
        input - Base64 encoded String
        Returns:
        decoded binary data
      • regexpReplace

        public static String regexpReplace​(String inputStr,
                                           String matchStr,
                                           String replaceStr,
                                           int matchStartPos,
                                           int occurence,
                                           String flag)
        Replace a regular expression pattern. If matchStr is not found, inputStr will be returned. By default, all occurences of match pattern in the input string will be replaced. Default matching pattern is case sensitive.
        Parameters:
        inputStr - Input string to apply the regexpReplace
        matchStr - Regexp or string to match against inputStr
        replaceStr - Regexp or string to replace if matchStr is found
        matchStartPos - Index of inputStr from where matching should start. Default is 0.
        occurence - Controls which occurence of the matched pattern must be replaced. Counting starts at 0. Default is -1
        flag - Single character flag that controls how the regex finds matches in inputStr. If an incorrect flag is specified, the function applies default case sensitive match. Only one flag can be specified. Supported flags: i -> Case insensitive
        Returns:
        replaced input string
      • regexpReplace

        public static String regexpReplace​(String inputStr,
                                           String matchStr,
                                           String replaceStr)
        See #regexpReplace(String, String, String, int, int, String). Matches against entire inputStr and replaces all occurences. Match is performed in case-sensitive mode.
        Parameters:
        inputStr - Input string to apply the regexpReplace
        matchStr - Regexp or string to match against inputStr
        replaceStr - Regexp or string to replace if matchStr is found
        Returns:
        replaced input string
      • regexpReplace

        public static String regexpReplace​(String inputStr,
                                           String matchStr,
                                           String replaceStr,
                                           int matchStartPos)
        See #regexpReplace(String, String, String, int, int, String). Matches against entire inputStr and replaces all occurences. Match is performed in case-sensitive mode.
        Parameters:
        inputStr - Input string to apply the regexpReplace
        matchStr - Regexp or string to match against inputStr
        replaceStr - Regexp or string to replace if matchStr is found
        matchStartPos - Index of inputStr from where matching should start. Default is 0.
        Returns:
        replaced input string
      • regexpReplace

        public static String regexpReplace​(String inputStr,
                                           String matchStr,
                                           String replaceStr,
                                           int matchStartPos,
                                           int occurence)
        See #regexpReplace(String, String, String, int, int, String). Match is performed in case-sensitive mode.
        Parameters:
        inputStr - Input string to apply the regexpReplace
        matchStr - Regexp or string to match against inputStr
        replaceStr - Regexp or string to replace if matchStr is found
        matchStartPos - Index of inputStr from where matching should start. Default is 0.
        occurence - Controls which occurence of the matched pattern must be replaced. Counting starts at 0. Default is -1
        Returns:
        replaced input string