Search
Varnish Cache Plus

Strings (str)

Description

The string vmod implements various helper function to manipulate strings.

API

len

INT len(STRING S)

Returns the number of ascii characters in S, or -1 if S is null.

Arguments:

  • S accepts type STRING

Type: Function

Returns: Int

startswith

BOOL startswith(STRING S1, STRING S2)

Returns true if S1 starts with S2.

Arguments:

  • S1 accepts type STRING

  • S2 accepts type STRING

Type: Function

Returns: Bool

endswith

BOOL endswith(STRING S1, STRING S2)

Returns true if S1 ends with S2.

Arguments:

  • S1 accepts type STRING

  • S2 accepts type STRING

Type: Function

Returns: Bool

contains

BOOL contains(STRING S1, STRING S2)

Returns true if S1 contains S2.

Arguments:

  • S1 accepts type STRING

  • S2 accepts type STRING

Type: Function

Returns: Bool

substr

STRING substr(STRING S, INT N, INT OFFSET = 0)

Returns a substring that is at most abs(N) characters long, from the character at the OFFSET position (negative OFFSET counts from the back of the string). If N is positive, the substring is extracted right of OFFSET, left otherwise.

Example:

  • substr(“0123456789abcdef”, 2, 5) -> “56”, 2 characters from the 5th one
  • substr(“0123456789abcdef”, 2, -5) -> “be”, 2 characters from the 5th to last one
  • substr(“0123456789abcdef”, -2, 5) -> “45”, 2 characters to the 5th one
  • substr(“0123456789abcdef”, -2, -5) -> “ab”, 2 characters to the 5th to last one
  • substr(“0123456789abcdef”, 10, 20) -> “abcdef”, truncated because the end of the string was reached.

If S in NULL, NULL is returned.

Arguments:

  • S accepts type STRING

  • N accepts type INT

  • OFFSET accepts type INT with a default value of 0 optional

Type: Function

Returns: String

reverse

STRING reverse(STRING S)

Reverse S.

Arguments:

  • S accepts type STRING

Type: Function

Returns: String

split

STRING split(STRING S, INT n, STRING SEP = " \t")

Split s and return the n-th token. Characters in SEP are separators. A negative N indicate “from the end of the string”.

Arguments:

  • S accepts type STRING

  • n accepts type INT

  • SEP accepts type STRING with a default value of \t optional

Type: Function

Returns: String

token_intersect

BOOL token_intersect(STRING str1, STRING str2, [STRING separators])

Checks if the set tokens in one string intersects the set of tokens in the other string. In other words, it returns true if there is a token which exists in both strings.

If the separators argument is not supplied, the space and comma characters are the separators. Empty tokens are ignored in both strings.

Arguments:

  • str1 accepts type STRING

  • str2 accepts type STRING

  • separators accepts type STRING

Type: Function

Returns: Bool

Availability

The str VMOD is available in Varnish Cache Plus version 6.0.4r3 and later.