Class StringFunctions
- java.lang.Object
-
- org.apache.pinot.common.function.scalar.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 Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringchr(int codepoint)static intcodepoint(String input)static Stringconcat(String input1, String input2, String seperator)Join two input string with seperator in betweenstatic booleancontains(String input, String substring)static StringdecodeUrl(String input)static StringencodeUrl(String input)static booleanendsWith(String input, String suffix)static byte[]fromBase64(String input)static StringfromUtf8(byte[] input)static inthammingDistance(String input1, String input2)static StringleftSubStr(String input, int length)static intlength(String input)static Stringlower(String input)static Stringlpad(String input, int size, String pad)static Stringltrim(String input)static Stringnormalize(String input)static Stringnormalize(String input, String form)static StringregexpExtract(String value, String regexp)static StringregexpExtract(String value, String regexp, int group)static StringregexpExtract(String value, String regexp, int group, String defaultValue)Regular expression that extract first matched substring.static StringregexpReplace(String inputStr, String matchStr, String replaceStr)See #regexpReplace(String, String, String, int, int, String).static StringregexpReplace(String inputStr, String matchStr, String replaceStr, int matchStartPos)See #regexpReplace(String, String, String, int, int, String).static StringregexpReplace(String inputStr, String matchStr, String replaceStr, int matchStartPos, int occurence)See #regexpReplace(String, String, String, int, int, String).static StringregexpReplace(String inputStr, String matchStr, String replaceStr, int matchStartPos, int occurence, String flag)Replace a regular expression pattern.static Stringremove(String input, String search)static Stringrepeat(String input, int times)static Stringrepeat(String input, String sep, int times)static Stringreplace(String input, String find, String substitute)static Stringreverse(String input)static StringrightSubStr(String input, int length)static Stringrpad(String input, int size, String pad)static Stringrtrim(String input)static String[]split(String input, String delimiter)static String[]split(String input, String delimiter, int limit)static StringsplitPart(String input, String delimiter, int index)static booleanstartsWith(String input, String prefix)static intstrcmp(String input1, String input2)Compare input strings lexicographically.static intstrpos(String input, String find)static intstrpos(String input, String find, int instance)static intstrrpos(String input, String find)static intstrrpos(String input, String find, int instance)static Stringsubstr(String input, int beginIndex)static Stringsubstr(String input, int beginIndex, int endIndex)Returns the substring of the main string from beginIndex to endIndex.static byte[]toAscii(String input)static StringtoBase64(byte[] input)static byte[]toUtf8(String input)static Stringtrim(String input)static Stringtrim(String end, String characters, String value)Standard SQL trim function.static Stringupper(String input)
-
-
-
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
-
upper
public static String upper(String input)
- Parameters:
input-- Returns:
- string in upper case format
- See Also:
String.toUpperCase()
-
substr
public static String substr(String input, int beginIndex)
- Parameters:
input- Parent stringbeginIndex- 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 stringbeginIndex- index from which substring should be createdendIndex- 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|TRAILINGcharacters- characters to be trimmed offvalue- 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 valueregexp- regular expressiongroup- 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 findinstance- 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 findinstance- 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)
-
replace
public static String replace(String input, String find, String substitute)
- Parameters:
input-find- target substring to replacesubstitute- new substring to be replaced with target- See Also:
String.replaceAll(String, String)
-
rpad
public static String rpad(String input, int size, String pad)
- Parameters:
input-size- final size of the stringpad- 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 stringpad- 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
-
normalize
public static String normalize(String input)
- Parameters:
input-- Returns:
- transforms string with NFC normalization form.
- See Also:
Normalizer.normalize(CharSequence, Normalizer.Form)
-
normalize
public static String normalize(String input, String form)
- Parameters:
input-form-- Returns:
- transforms string with the specified normalization form
- See Also:
Normalizer.normalize(CharSequence, Normalizer.Form)
-
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.
-
encodeUrl
public static String encodeUrl(String input) throws UnsupportedEncodingException
- Parameters:
input- plaintext string- Returns:
- url encoded string
- Throws:
UnsupportedEncodingException
-
decodeUrl
public static String decodeUrl(String input) throws UnsupportedEncodingException
- Parameters:
input- url encoded string- Returns:
- plaintext string
- Throws:
UnsupportedEncodingException
-
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 regexpReplacematchStr- Regexp or string to match against inputStrreplaceStr- Regexp or string to replace if matchStr is foundmatchStartPos- 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 -1flag- 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 regexpReplacematchStr- Regexp or string to match against inputStrreplaceStr- 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 regexpReplacematchStr- Regexp or string to match against inputStrreplaceStr- Regexp or string to replace if matchStr is foundmatchStartPos- 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 regexpReplacematchStr- Regexp or string to match against inputStrreplaceStr- Regexp or string to replace if matchStr is foundmatchStartPos- 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
-
-