The following String functions are available in DataScripts:
String |
Description |
---|---|
Search for string in beginning of a string. |
|
Search contains a string in another string. |
|
Search for string at the end of a string. |
|
Returns number of characters in string. |
|
Change a string to lower case. |
|
Parses a string into substrings. |
|
Extract a sub-string from a string. |
|
Change a string to upper case. |
string.beginswith
Function |
string.beginswith(source, target) |
Description |
Check if the source string begins with the target string. |
Events |
All. |
Parameter |
source is the string to be checked. target is the string, the source is matched against. |
Returns |
Boolean |
Example |
If the path starts with /sales/ redirect to a new URL. path = avi.http.get_path() if string.beginswith(path, "/sales/") then avi.http.redirect("http://sales.test.com/index.html") end |
string.contains
Function |
string.contains(source, target) |
Description |
Check if the source string contains the target string. |
Events |
All. |
Parameter |
source is the string to be checked. target is the string, the source is matched against. |
Returns |
Boolean true if the source string contains the target string, else false. |
Example |
If the path contains /finance/ prompt client for authentication. path = avi.http.get_path() if string.contains(path, "/finance/") then avi.http.response(401) end |
string.endswith
Function |
string.endswith(source, target) |
Description |
Check if the source string ends with the target string. |
Events |
All. |
Parameter |
source is the string to be checked. target is the string, the source is matched against. |
Returns |
Boolean |
Example |
If the path ends with /secure/ forbid access. path = avi.http.get_path() if string.endswith(path, "/secure/") then avi.http.response(403) end |
string.len
Function |
string.len(source) |
Description |
Returns the number of characters of the source string. |
Events |
All. |
Parameter |
source is the string to be checked. |
Returns |
Numeric value. |
Example |
Log the number of characters from the path of the client's request. avi.vs.log(string.len(avi.http.get_path())) |
string.lower
Function |
string.lower(source) |
Description |
Converts the source string to all lowercase characters. The VMware Avi Load Balancer operators are case sensitive, a does not equal A. |
Events |
All. |
Parameter |
source is the string to be converted. |
Returns |
The source string in lowercase. |
Example |
Evaluate a string from the HTTP path as all lower case characters. if string.lower(avi.http.get_path()) == "/sales" then ... |
string.split
Function |
string.split (source_string, delimiter) source_string:split (delimiter) |
Description |
Using a user-supplied delimiter, parses a string into substrings, and returns them in a Lua table of dimension equal to the number of substrings found. |
Events |
All. |
Parameter |
source_string is the string to split. delimiter is a single character by which to parse source_string. |
Returns |
If either source_string or delimiter is nil, returns NIL. Otherwise, returns a Lua table with elements corresponding to source_string, but split by each occurrence of delimiter. If there are no occurrences of delimiter in source_string, the table returned will have only one element, namely, the full source_string. |
Example |
URI example example_string = "www.example.com/this/is/an/example/uri" split_results = string.split(example_string, "/") avi.vs.log(split_results[6]) In the above example, split_results is a table containing: split_results[1] → "www.example.com" split_results[2] → "this" split_results[3] → "is" split_results[4] → "an" split_results[5] → "example" split_results[6] → "uri" Consequently, the character string "uri" will be logged in the request logs. Split miss example example_string = "This string is just a sentence." split_results = example_string:split(":") avi.vs.log(split_results[1]) |
1. In the above function reference, the prefix of the function name is dropped.
2. As example_string does not contain the colon delimiter, all its characters will be logged in the request logs, i.e., the output will be - "This string is just a sentence."
string.sub
Function |
string.sub(source, begin, [end]) |
Description |
Extracts part of a string from a source string. Characters are count starting with a 1 for the first character, 2 for the second, etc. Characters may also be negative. So -1 is the last character of the source string, -2 is the second to last, etc. |
Events |
All. |
Parameter |
source is the string to be inspected. begin is the first character for the desired result. end is the final character for the desired result. If no end is specified, the new string will be from the begin till the last character. |
Returns |
A portion of the original string. Can return the entire original string, or |
Example 1 |
Extract the 2nd through the 4th characters. var = "abcdef" avi.vs.log(string.sub(var, 2, 4)) DataScript Log: [string "Test-Rule"]:2: bcd |
Example 2 |
Extract characters from the string, starting from the 99th to last till the 2nd to last. var = "abcdef" avi.vs.log(string.sub(var, -99, 2)) DataScript Log: [string "Test-Rule"]:2: abcde |
Example 3 |
Update a variable with the altered string. A path always starts with a ' path = avi.http.get_path() path = string.sub(path, 2) if path == "index.html" ... |
Example 4 |
The following example can be used to prevent a Shell Shock attack. This attack embeds a client header which starts with headers = avi.http.get_header() for key,val in pairs(headers) do if #val > 2 and string.sub(val, 1, 2) == "()" then avi.http.close_conn() end end |
string.upper
Function |
string.upper(source) |
Description |
Converts the source string to all uppercase characters. The VMware Avi Load Balancer operators are case sensitive. An "a" does not equal "A". This command enables case insensitivity. |
Events |
All. |
Parameter |
source is the string to be converted. |
Returns |
The source string in uppercase. |
Example |
Evaluate the client's path as upper case characters. Client might have sent if string.upper(avi.http.get_path()) == "/SALES" then ... |