All Products
Search
Document Center

Server Load Balancer:String functions

Last Updated:Apr 01, 2026

This topic describes the syntax, features, parameters, and return values of string functions. This topic also provides examples of these functions.

substr | concat | format | upper | lower | len | byte | match_re | capture_re | gsub_re | split | split_as_key | tohex | tobin | tostring | tochar | reverse | find | trim

substr

Syntax: substr(s STRING, i INTEGER, j INTEGER) → STRING

Extracts the substring s[i, j] from s. Positions are 1-based. Use -1 to refer to the last character.

ParameterTypeDescription
sstringThe source string.
iintegerStart position (1-based). -1 = last character.
jintegerEnd position (1-based). -1 = last character.

Returns: The extracted substring.

Example: Check whether a URI ends with .m3u8.

Method 1 — use -1 as the end position:

if eq(substr($uri, -5, -1), '.m3u8') {
    say(concat($uri, ' is .m3u8'))
}

Method 2 — use the string length as the end position:

uri_len = len($uri)
if eq(substr($uri, -5, uri_len), '.m3u8') {
    say(concat($uri, ' is .m3u8'))
}

concat

Syntax: concat(s1, ...) → STRING

Concatenates one or more values into a single string. Numeric values are accepted.

ParameterTypeDescription
s1, ...string or numericThe values to concatenate.

Returns: The concatenated string.

Example:

Method 1 — use -1 as the end position:

if eq(substr($uri, -5, -1), '.m3u8') {
    say(concat($uri, ' is .m3u8'))
}

Method 2 — use the string length as the end position:

uri_len = len($uri)
if eq(substr($uri, -5, uri_len), '.m3u8') {
    say(concat($uri, ' is .m3u8'))
}

format

Syntax: format(fmt STRING, ...) → STRING

Formats one or more values using a format string that follows the sprintf convention from ISO C. The format string syntax is %[parameter][flag][field width][precision]specifier.

Supported specifiers:

SpecifierDescription
%%Literal percent sign (%)
%cInteger to ASCII character
%dInteger to decimal
%fFloating-point number
%oInteger to octal
%sString
%xInteger to lowercase hexadecimal
%XInteger to uppercase hexadecimal
ParameterTypeDescription
fmtstringThe format string.
...anyVariable number of values to format.

Returns: A formatted string.

Example:

say(concat('format:', format('%%%s$%.2s$%s$%c$%d$%2.2f$%.2o$%x$%X', 'format', 3.1415926, true, 95, 3.1415926, 3.1415926, 3.1415926, 10, 10)))

Output:

format:%format$3.$true$_$3$3.14$03$a$A

upper

Syntax: upper(s STRING) → STRING

Converts all characters in s to uppercase.

ParameterTypeDescription
sstringThe string to convert.

Returns: The uppercase version of s.

Example:

mystr = 'Hello, AScript'
say(upper(mystr))

Output:

HELLO, ASCRIPT

lower

Syntax: lower(s STRING) → STRING

Converts all characters in s to lowercase.

ParameterTypeDescription
sstringThe string to convert.

Returns: The lowercase version of s.

Example:

mystr = 'Hello, AScript'
say(lower(mystr))

Output:

hello, ascript

len

Syntax: len(s STRING) → INTEGER

Returns the number of characters in s.

ParameterTypeDescription
sstringThe string to measure.

Returns: The length of s as an integer.

Example:

say(len('hello'))

Output:

5

byte

Syntax: byte(c STRING) → NUMERIC

Returns the ASCII value of a single character.

ParameterTypeDescription
cstringA single character.

Returns: The ASCII value as a numeric.

Example:

say(byte('a'))
say(byte('A'))

Output:

97
65

match_re

Syntax: match_re(s STRING, p STRING, [o STRING]) → BOOLEAN

Tests whether s matches the regular expression p using the Perl Compatible Regular Expressions (PCRE) engine. For PCRE syntax details, see PCRE syntax.

ParameterTypeDescription
sstringThe string to test.
pstringThe regular expression.
ostring(Optional) The regex engine option.

Returns: true if s matches p; otherwise false.

To extract substrings from a match rather than test for a match, use capture_re instead.

Example: Add response headers when a URL matches specific patterns.

url = concat('http://', $host, $uri)
m1 = match_re(url, 'http://.*\.dslex\.com/.*')
m2 = match_re(url, '^http://.*\.alibaba\.com\.cn/.*\.d\\.html(\?.*)?$')
m3 = match_re(url, '^http://.*.test.dslex.com/.*\.d\.html(\?.*)?$')
m4 = match_re(url, '^http://.*\.alibaba\.com\.cn/zt_d/')
m5 = match_re(url, '^http://tech.alibaba.com.cn/zt_d/we2015/?$')
m6 = match_re($args, 'from=wap1$')
m7 = match_re($args, 'from=comos1$')

if and(m1, or(m2, m3), not(m4), not(m5), or(not(m6), not(m7))) {
    add_rsp_header('USER-DEFINED-1', 'hit1')
    add_rsp_header('USER-DEFINED-2', 'hit2')
}

capture_re

Syntax: capture_re(s STRING, p STRING, [init INTEGER]) → DICTIONARY

Matches s against p and returns all captured substrings. For PCRE syntax details, see PCRE syntax.

ParameterTypeDescription
sstringThe string to match.
pstringThe regular expression.
initinteger(Optional) The position to start matching, counting from 1.

Returns: A dictionary of matched substrings if s matches p; an empty dictionary otherwise.

Example: Validate a URI by extracting and verifying its segments.

pcs = capture_re($request_uri,'^/([^/]+)/([^/]+)([^?]+)\?(.*)')
sec1 = get(pcs, 1)
sec2 = get(pcs, 2)
sec3 = get(pcs, 3)
if or(not(sec1), not(sec2), not(sec3)) {
   add_rsp_header('X-TENGINE-ERROR', 'auth failed - missing necessary uri set')
   exit(403)
}
digest = md5(concat(sec1, sec3))
if ne(digest, sec2) {
    add_rsp_header('X-TENGINE-ERROR', 'auth failed - invalid digest')
    exit(403)
}

gsub_re

Syntax: gsub_re(subject STRING, regex STRING, replace STRING, [option STRING]) → STRING

Replaces all substrings in subject that match regex with replace. For PCRE syntax details, see PCRE syntax.

The replace string can reference matched content:

  • $0 — the entire matched substring

  • $N — the substring matched by the Nth capturing group ()

ParameterTypeDescription
subjectstringThe source string.
regexstringThe regular expression.
replacestringThe replacement string. Supports $0 and $N back-references.
optionstring(Optional) The regex engine option.

Returns: The string after all matches have been replaced.

To test whether a string matches a pattern without replacing it, use match_re instead.

Example:

subject = 'Hello, Es'
regex = '([a-zA-Z])[a-z]+'
replace = '[$0,$1]'
add_rsp_header('X-DEBUG-GSUB-RE', gsub_re(subject, regex, replace))

Output:

X-DEBUG-GSUB-RE: [Hello,H], [Es,E]

split

Syntax: split(s STRING, [sep STRING]) → DICTIONARY

Splits s into substrings at each occurrence of sep and returns them as a dictionary. Keys are sequential integers starting from 1 — for example, [1]=xx, [2]=yy.

If sep is omitted, s is split on whitespace characters (spaces and tab characters \t).

ParameterTypeDescription
sstringThe string to split.
sepstring(Optional) The separator. Defaults to whitespace.

Returns: A dictionary where each key is an integer starting from 1.

To use each split element as its own key instead of a sequential integer, use split_as_key.

Example:

if $arg_from {
    t = split($arg_from, ',')
    # Retrieve the first element.
    if get(t, 1) {
        say(concat('[1]=', get(t, 1)))
    }
    # Retrieve the second element.
    if get(t, 2) {
        say(concat('[2]=', get(t, 2)))
    }
    # Retrieve the third element.
    if get(t, 3) {
        say(concat('[3]=', get(t, 3)))
    }
}

Request:

?from=xx1,xx2,xx3

Response:

[1]=xx1
[2]=xx2
[3]=xx3

split_as_key

Syntax: split_as_key(s STRING, [sep STRING]) → DICTIONARY

Splits s the same way as split, but uses each split element as its own dictionary key instead of a sequential integer.

ParameterTypeDescription
sstringThe string to split.
sepstring(Optional) The separator. Defaults to whitespace.

Returns: A dictionary where each key is the element value itself.

Example:

def echo_each(k, v, u) {
    # Retrieve the value for the given key in the dictionary.
    s = concat(k, '=', v, ' u=', get(u, 1))
    say(s)
}
if $arg_from {
    t = split_as_key($arg_from, ',')
    # Traverse each element in the dictionary.
    foreach(t, echo_each, ['hi,ascript'])
}

Request:

?from=xx1,xx2,xx3

Response:

xx2=xx2 u=hi,ascript
xx1=xx1 u=hi,ascript
xx3=xx3 u=hi,ascript

tohex

Syntax: tohex(s STRING) → STRING

Converts a string to a hexadecimal string.

ParameterTypeDescription
sstringThe string to convert.

Returns: A hexadecimal string.

Example:

digest = 'xxxx'
add_rsp_header('X-AScript-TOHEX', tohex(digest))

Output:

X-AScript-TOHEX: 78787878

tobin

Syntax: tobin(str STRING) → STRING

Converts a hexadecimal string to the corresponding ASCII string. The input is case-insensitive.

ParameterTypeDescription
strstringA string of hexadecimal digits (case-insensitive).

Returns: The ASCII string decoded from the hex input.

Example:

say(concat('tobin:', tobin('2F2F')))

Output:

tobin://

tostring

Syntax: tostring(a ANY) → STRING

Converts a value of any type to its string representation.

ParameterTypeDescription
aanyThe value to convert.

Returns: The string representation of a.

Example:

s = tostring(123)
add_rsp_header('X-DSL-TOSTRING', s)

Output:

X-DSL-TOSTRING: 123

tochar

Syntax: tochar(n1 INTEGER, n2 INTEGER, ...) → STRING

Converts one or more integers to their corresponding ASCII characters and returns them as a single string. For example, 48 maps to '0'. The length of the returned string is the total length of all parameters after conversion.

ParameterTypeDescription
n1, n2, ...integerThe integers to convert. One or more values accepted.

Returns: A string built from the ASCII characters corresponding to the input integers.

Example:

say(tochar(97))
say(tochar(97,98))

Output:

a
ab

reverse

Syntax: reverse(str STRING) → STRING

Reverses the character order of str.

ParameterTypeDescription
strstringThe string to reverse.

Returns: The reversed string. Data type: CHAR.

Example:

say(reverse('hello'))

Output:

olleh

find

Syntax: string.find(s STRING, substr STRING, [pos NUMERIC]) → ARRAY

Searches for substr within s, starting at position pos. Positions are 1-based. A negative pos counts from the end of s. The default value of pos is 1.

ParameterTypeDescription
sstringThe string to search.
substrstringThe substring to find.
posnumeric(Optional) The position to start the search. Default: 1. Negative values count from the end.

Returns:

  • An array [start, end] if substr is found — index 1 is the start position and index 2 is the end position.

  • An empty array if substr is not found.

Example:

str = 'hello dsl'
# Expected output: 7
add_rsp_header('string-find()-start', tostring(get(find(str, 'dsl'), 1)))
str = 'hello dsl 12'
# Expected output: 9
add_rsp_header('string-find()-end', tostring(get(find(str, 'dsl'), 2)))
str = 'hello dsl'
# Expected output: 7
add_rsp_header('string-find()-tail-start', tostring(get(find(str, 'dsl', -6), 1)))
str = 'hello dsl 12'
# Expected output: 9
add_rsp_header('string-find()-tail-end', tostring(get(find(str, 'dsl', -6), 2)))

Output:

string-find()-start:7
string-find()-end:9
string-find()-tail-start:7
string-find()-tail-end:9

trim

Syntax: trim(s STRING, [loc STRING]) → STRING

Removes whitespace from the beginning, end, or both ends of s.

ParameterTypeDescription
sstringThe string to trim.
locstring(Optional) Where to remove whitespace. Default: both. Valid values: both, left, right.

Returns: The trimmed string.

Example:

say(concat('trim():', trim(' abcd ')))
say(concat('trim(left):', trim(' abcd ', 'left')))
say(concat('trim(right):', trim(' abcd ', 'right')))

Output:

trim():abcd
trim(left):abcd
trim(right): abcd