Working with strings
substring() | endswith() | startswith() | contains() | len() | concat() | splitter() | lower() | upper() | strip() | capitalize() | title() | normalizePhone() | replace() | base64() | base64decode() | urlencode() | urldecode() | hmac_hexdigest() | select_random() | tg_escape()
LEGEND:
! - Required parameters
Description
substring(str, n1, n2) - to trim a string
Parameters:
! str - original string
! n1 - number of characters to trim from the left (> 0)
n2 - number of characters to trim from the right (< 0)
endswith(str, substr) - to check if the string ends with the given substring
Parameters:
! str - original string - "where to search"
! substr - строка поиска - "what to search"
startswith(str, substr) - to check if the string starts with the given substring
Parameters:
! str - original string - "where to search"
! substr - строка поиска - "what to search"
contains(str, substr,registr) – to check if the first string contains the second string
Parameters:
! str - original string - "where to search"
! substr - строка поиска - "what to search"
registr - a flag indicating whether to consider case sensitivity (False means case should not be considered)
len(str) – to count the number of characters in a string.
Parameters:
! str - original string
concat(str1, str2) – to concatenate (join) the strings passed as parameters
Parameters:
! str1 - string 1
! str2 - string 2
splitter(str, s, n) - to split a string into parts. The function returns an array of elements
Parameters:
! str - original string
! s - string delimiter
n - maximum number of elements
lower(str) – to convert a string to lowercase
Parameters:
! str - original string
upper(str) - to convert a string to uppercase
Parameters:
! str - original string
strip(str) - to trim whitespace from both ends of a string
Parameters:
! str - original string
capitalize(str) - to replace the first character of a string with its uppercase equivalent (capitalizes the first letter of the word).
Parameters:
! str - original string
title(str) - to convert each word in the string str so that the first letter is uppercase and others are lowercase
normalizePhone(str) - to formate a phone number into a standard form: removes all non-digit characters and replaces the starting digit 8 with 7 if present
Parameters:
! str - original string with a phone number
replace(str, s1, s2, n) - to replace a substring in a string with another substring
Parameters:
! str - original string
! s1 - substring to be replaced
! s2 - substring to replace with
n - number of replacements
base64(str) – to encode a string in base64 format
Parameters:
! str - original string
base64decode(str) - to decode base64 back to a string
Parameters:
! str - original string
urlencode(str) - to encode a string to make it safe for HTTP transmission
Parameters:
! str - original string
For examle: John Smith becomes John%20Smith, а Anna&Maria becomes Anna%26Maria
urldecode(str) - to decode a URL-encoded string
Parameters:
! str - original string
For example: John%20Smith will be translated as John Smith
hmac_hexdigest(secret_key, msg, hash_type) - to hash a string using 'sha256', 'md5', 'sha512', or 'sha1'
Parameters:
! secret_key - key ! msg - string to be hashed ! hash_type - hash type ( 'sha256', 'md5', 'sha512' or 'sha1')
select_random(str, s) - to select a random element from a delimited string. The first parameter is the string with elements, the second is the delimiter. The second parameter defaults to '|'.
Parameters:
! str - original string
! s - string delimiter (default is '|')
Example usage:
select_random('first element | second element | another element | and another one')
tg_escape(str) - to escape a variable and display it in a Telegram message with enabled markup. The function adds a backslash before the following characters: '_', '*', '[', ']', '(', ')', '~', '', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'
Parameters:
! str - original string
Examples
Let’s analyze the string trimming function:


Determining the length of the string:


A function that splits a string into parts is often needed when working with tables:


Phone number processing:


Replacing a substring in a string:


Code example for copying
/*Analyzing substring()*/
text = 'text for trimming'
a=substring(text, 4)
a1=substring(text, -4)
b=substring(text, 4, 6)
b1=substring(text, 0, 6)
c=substring(text, 0, -4)
d=substring(text, 4, -4)
/*working with len()*/
text = 'text to be trimmed'
a=len(text)
b=len("what a wonderful world!")
/*split the string into parts*/
elements = splitter('s, W, q', ',')
text='text1:text2:text3:text4:text5'
texts = splitter(text, ':',2)
/*title()*/
full name = John Smith
full name = title("#{full name}")
/*phone number processing*/
phone = normalizePhone("+971 50 123 4567")
/*substring replacement in a string*/
a=replace("wwww2222ww", "w", "e", 1)
Last updated