All Products
Search
Document Center

CDN:String functions

Last Updated:Dec 02, 2024

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

substr

The following table describes the details about this function.

Feature

Description

Syntax

substr(s, i, j)

Description

Extracts parts from a string.

Parameter

  • s: the source string.

  • i: the position to start extraction in the source string, counting from 1. A value of -1 specifies the rightmost character of the string. Data type: integer.

  • j: the position to end extraction in the source string, counting from 1. A value of -1 specifies the rightmost character of the string. Data type: integer.

Return value

Returns a substring s[i, j] that is extracted from the source string specified by s.

Example

//Note: The two methods that are used to determine whether a file is an M3U8 file.
if eq(substr($uri, -5, -1), '.m3u8') {
    say(concat($uri, ' is .m3u8'))
}

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

concat

The following table describes the details about this function.

Feature

Description

Syntax

concat(s1, ...)

Description

Concatenates strings.

Parameter

The strings that you want to concatenate. You can specify one or more strings. Numeric values are supported.

Return value

Returns a concatenated string.

Example

// Concatenates the following strings
str1 = 'hello'
str2 = ' '
str3 = 'world'
say(concat(str1, str2, str3))

Output:

  hello world

upper

The following table describes the details about this function.

Feature

Description

Syntax

upper(s)

Description

Converts a string to uppercase letters.

Parameter

s: the string that you want to convert.

Return value

Returns the string specified by the s parameter in uppercase letters.

Example

str = 'hello world'                                                                                                                                                                                   
say(upper(str)) 

Output:

HELLO WORLD

lower

The following table describes the details about this function.

Feature

Description

Syntax

lower(s)

Description

Converts a string to lowercase letters.

Parameter

s: the string that you want to convert.

Return value

Returns the string specified by the s parameter in lowercase letters.

Example

str = 'HELLO WORLD'
say(lower(str))

Output:

hello world

len

The following table describes the details about this function.

Feature

Description

Syntax

len(s)

Description

Queries the length of a string.

Parameter

s: the string that you want to measure.

Return value

Returns the length of the string specified by the s parameter. Data type: integer.

Example

str = 'hello world'
say(len(str)) // Returns the length of str. Here is 11.

byte

The following table describes the details about this function.

Feature

Description

Syntax

byte(c)

Description

Queries the ASCII value of a character.

Parameter

c: the character whose ASCII value you want to query. You can specify only one character.

Return value

Returns the ASCII value of the specified character. Data type: numeric.

Example

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

Output:

97
65

match_re

The following table describes the details about this function.

Feature

Description

Syntax

match_re(s, p [, o])

Description

Uses the Perl Compatible Regular Expressions (PCRE) engine for regular expression matching. For more information, see PCRE syntax.

Parameter

  • s: the string that you want to match. Data type: string.

  • p: the regular expression for matching. Data type: string.

  • o: the regular expression engine. Data type: string. This parameter is optional.

    • empty value: specifies that this function is case-sensitive.

    • i: specifies that this function is not case-sensitive.

Return value

If the string matches the regular expression, true is returned. Otherwise, false is returned.

Example

str = 'Hello'
// An exact match with "Hello"
say(match_re(str, 'Hello'))
// A case-insensitive match with "Hello" 
say(match_re(str, 'hello', i))
// Matches any string that ends with the letter "l"
say(match_re(str, '.*l$'))

Output:

true
true
false

capture_re

The following table describes the details about this function.

Feature

Description

Syntax

capture_re(s, p [,init])

Description

Captures the matches of a string and returns the matching substrings. For more information about PCRE, see PCRE syntax.

Parameter

  • s: the string that you want to match. Data type: string.

  • p: the regular expression for matching. Data type: string.

  • init: the position to start matching, counting from 1. Data type: integer.

Return value

Returns the matching substrings in the dictionary type if the string matches the regular expression. Otherwise, an empty dictionary is returned.

Example

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)
}                                                                                                                                                      

Response example

"X-TENGINE-ERROR:auth failed - missing necessary uri set"

gsub_re

The following table describes the details about this function.

Feature

Description

Syntax

gsub_re(subject, regex, replace [,option])

Description

Replaces all matches of a string and returns the string after the replacement. For more information about PCRE, see PCRE syntax.

Parameter

  • subject: the string that you want to match. Data type: string.

  • regex: the regular expression. Data type: string.

  • replace: the string for replacement. Data type: string.

    You can specify the replace parameter by using the matching substrings.

    • $0: specifies all substrings that match regex.

    • $N: specifies the substring that matches the Nth parenthesized subexpression () of regex.

  • option: the regular expression engine. Data type: string. This parameter is optional.

Return value

Replaces all substrings that match the specified regex parameter in the specified subject parameter with those specified by the replace parameter and returns the string after the replacement.

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))                                                                                                                                    

Response example

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

split

The following table describes the details about this function.

Feature

Description

Syntax

split(s [,sep])

Description

Splits a string into an array of substrings and returns the array.

Parameter

  • s: the string that you want to split. Data type: string.

  • sep: the separator that is used to split the string. Data type: string.

Return value

Returns an array of key-value pairs in the dictionary type. The value of the key parameter is a number that starts from 1, for example, [1]=xx and [2]=y. If sep is left empty, the string is split by whitespace characters. Whitespace characters include space characters and tab characters (\t).

Example

if $arg_from {
    t = split($arg_from, ',')
    if get(t, 1) {
        say(concat('[1]=', get(t, 1)))
    }
    if get(t, 2) {
        say(concat('[2]=', get(t, 1)))
    }
}                                                                                                                                

Request:

http://www.example.com?from=xx1,xx2,xx3

Output:

[1]=xx1
[2]=xx1

split_as_key

The following table describes the details about this function.

Feature

Description

Syntax

split_as_key(s [,sep])

Description

Splits a string into an array of substrings and returns the array.

Parameter

  • s: the string that you want to split. Data type: string.

  • sep: the separator that is used to split the string. Data type: string.

Return value

Returns response parameters in the same way as the split() function. However, the key parameter is named after each split element: Element 1 -> Element 2.

Example

def echo_each(k, v, u) {
    s = concat(k, '=', v, ' u=', get(u, 1))
    say(s) 
}
if $arg_from {
    t = split_as_key($arg_from, ',')
    foreach(t, echo_each, ['hi,dsl'])
}                                                                                                                                  

Request:

http://www.example.com?from=xx1,xx2,xx3

Output:

xx2=xx2 u=hi,dsl
xx1=xx1 u=hi,dsl
xx3=xx3 u=hi,dsl

tohex

The following table describes the details about this function.

Feature

Description

Syntax

tohex(s)

Description

Converts a string to a hexadecimal string.

Parameter

s: the string that you want to convert.

Return value

Returns a hexadecimal string that is converted from the string specified by the s parameter.

Example

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

Response example

"X-DSL-TOHEX: 4ad583af22c2e7d40c1c916b2920299155a46464"

tostring

The following table describes the details about this function.

Feature

Description

Syntax

tostring(a)

Description

Converts data of any type to a string.

Parameter

a: the data that you want to convert. Data type: any type.

Return value

Returns a string that is converted from the value specified by the a parameter.

Example

say(tostring(123))

Output:

123

tochar

The following table describes the details about this function.

Feature

Description

Syntax

tochar(n1, n2, ...)

Description

  • Converts one or more internal integers (ASCII values) to a string. For example, 48 corresponds to the character "0".

  • The length of the returned string is based on the number of specified parameters.

Parameter

nX: the integers that you want to convert. You can specify one or more integers.

Return value

Returns a string that is converted from integers.

Example

say(tochar(97))
say(tochar(65))

Output:

a
A

reverse

The following table describes the details about this function.

Feature

Description

Syntax

reverse(str)

Description

Reverses a string.

Parameter

str: the string that you want to reverse.

Return value

Returns a string reversed from the specified string.

Example

say(reverse('hello'))

Output:

olleh

find

The following table describes the details about this function.

Feature

Description

Syntax

string.find (s, substr, pos)

Description

Searches for a substring in a specified string.

Parameter

  • s: the string that you want to search.

  • substr: the substring that you want to search.

  • pos: the position where the search starts. Data type: numeric. This parameter is optional. You can specify a negative integer. The default value is 1.

Return value

  • Returns an array if the specified substring is found.

    • Index 1 indicates the position where the search starts.

    • Index 2 indicates the position where the search ends.

  • Returns an empty array if the specified substring is not found.

Example

str = 'hello world'
say(concat('r start pos: ',get(find(str, 'r'), 1)))
say(concat('r end pos: ',get(find(str, 'r'), 2)))
say(concat('rl start pos: ',get(find(str, 'rl'), 1)))
say(concat('rl end pos: ',get(find(str, 'rl'), 2)))

Output:

r start pos: 9
r end pos: 9
rl start pos: 9
rl end pos: 10

format

The following table describes the details about this function.

Feature

Description

Syntax

format(fmt, ···)

Description

Formats the values of one or more parameters. The format string is the first parameter, which must specify a string. The format string follows the specification of the sprintf parameter used by functions in the C programming language.

The syntax of a format string is: %[parameter][flag][field width][.precision]specifier.

  • %%: prints literal percentage signs (%).

  • %c: converts integers into ASCII characters.

  • %d: coverts integers into decimal numbers.

  • %f: converts N-precision numbers into floating point numbers.

  • %o: converts integers into octal numbers.

  • %s: converts integers into strings.

  • %x: converts integers into hexadecimal numbers in lowercase letters.

  • %X: converts integers into hexadecimal numbers in uppercase letters.

Parameter

  • fmt: the string type. This parameter specifies a format string.

  • The variable number of parameters: any type.

Return value

Returns an ACSII 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

tobin

The following table describes the details about this function.

Feature

Description

Syntax

tobin(str)

Description

Converts a hexadecimal string to an ASCII string.

Parameter

str: the hexadecimal string that you want to convert. It is not case-sensitive.

Return value

Returns an ACSII string.

Example

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

Output:

tobin://

trim

The following table describes the details about this function.

Feature

Description

Syntax

trim(s, [, loc])

Description

Removes all whitespace characters before or after the string specified by the s parameter, and returns a string with the specified whitespace characters removed.

Parameter

  • s: the string.

  • loc: the default value is both. This parameter is optional. Valid values:

    • both: removes the whitespace characters before and after the string.

    • left: removes only the whitespace characters before the string.

    • right: removes only the whitespace characters after the string.

Return value

Returns a string with the specified whitespace characters removed.

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