String functions allow you to analyze, convert, and manipulate text data.
str.lenDetermines the length of a string in characters.
Signature: str.len(str string) int
Arguments: str — the string whose length you want to determine.
Result: An integer equal to the number of characters in the string.
Example of use:
$str = “Some string”
$len = str.len($str) // $len will contain 15
str.lowerConverts all characters in the string to lowercase.
Signature: str.lower(str string) string
Arguments: str — the string to be converted.
Result: A string in which all characters are lowercase.
Example of use:
$str = “StRiNg”
$lower = str.lower($str) // $lower will contain “string”
str.upperConverts all characters in the string to uppercase.
Signature: str.upper(str string) string
Arguments: str — the string to be converted.
Result: A string in which all characters are uppercase.
Example of use:
$str = “StRiNg”
$upper = str.upper($str) // $lower will contain “STRING”
str.ucfirstConverts the first character of a string to uppercase.
Signature: str.ucfirst(str string) string
Arguments: str — the string whose first character needs to be converted.
Result: A string with the first character in uppercase.
Example of use:
$str = str.ucfirst(“string”) // $str will contain “String”
str.lcfirstConverts the first character of a string to lowercase.
Signature: str.lcfirst(str string) string
Arguments: str — the string whose first character needs to be converted.
Result: A string with the first character in lowercase.
Example of use:
$str = str.lcfirst(“String”) // $str will contain “string”
str.letterReturns a string character at the specified position.
Signature: str.letter(str string, index int) string
Arguments:
str — a string whose character needs to be retrieved.index — the position of the character in the string (starting from 0). If negative, counting starts from the end of the string.Result: A string containing the character at the specified position, or an empty string if no character with that position exists.
Example of use:
$str = “Word”
$firstLetter = str.letter($str, 0) // First letter
$lastLetter = str.letter($str, -1) // Last letter
str.concatConcatenates two strings into one.
Signature: str.concat(strings ...string) string
Arguments: strings — strings to be concatenated.
Result: A new string consisting of the first string with the second string appended to the right.
Example of use:
$str1 = “one”
$str2 = “two”
$str = str.concat($str1, $str2) // $str will contain “onetwo”
str.formatFormats a string by substituting values into specified placeholders.
Signature: str.format(format string, values… any) string
Arguments:
format — a format string consisting of zero or more directives: regular characters (except %) that are simply output without change, and conversion specifiers, each of which requires its own parameter to be passed. A conversion specifier corresponds to the prototype: %[argnum$][flags][width][.precision]specifier.values — values that will be substituted into the format.Argument number (argnum): An integer followed by a $ sign to indicate which numeric argument to process during conversion.
Flags: Flags change the formatting behavior:
- — left-align (default is right-align).+ — always display the sign of the number (+ or -).0 — pad the number with zeros on the left.'symbol — pad with the specified symbol. Width: Specifies the minimum number of characters for outputting the value. If the value is shorter, it will be padded with spaces or the specified symbol.
Precision: The precision in string formatting is specified after . and determines how many characters will be displayed or how the value will be rounded.
f, e, E, F), the precision specifies the number of digits after the decimal point. The default is 6 digits after the decimal point.g, G, h, H, precision determines the maximum number of significant digits.s), precision limits the maximum number of characters that will be displayed.If no precision value is specified after the dot, the precision is assumed to be 0.
You can also use the * symbol instead of a specific number to specify the precision. In this case, the precision value is passed as an additional argument before the main value.
Specifiers: Define the type and format of the output value:
% — percent symbol. No arguments are required.b — the argument is treated as an integer and printed in binary representation.c — the argument is treated as an integer and printed as a character from the ASCII table with the corresponding code.d — the argument is treated as an integer and printed as a signed integer.e — the argument is considered a number in scientific notation (i.e., 1.2e+2).E — the argument is similar to the e specifier, but with an uppercase letter (i.e., 1.2E+2).f — the argument is considered a floating point number (taking into account the locale).F — the argument is considered a floating point number (without taking into account the locale).g — General format. Let P be equal to the precision if the precision is not zero, 6 if the precision is not specified, or 1 if the precision is 0. Then, if the conversion with style “E” has an exponent X: If P > X ≥ −4, the conversion will be in style ‘f’ and the precision will be P - (X + 1). Otherwise, the conversion will be in style “e” and the precision will be P - 1.G — the argument is similar to the g specifier, but uses the E and f specifiers.h — the argument is similar to the g specifier, but uses the F specifier.H — the argument is similar to the g specifier, but uses the E and F specifiers.o — the argument is treated as an integer and printed in octal representation.s — the argument is treated and printed as a string.u — the argument is treated as an integer and printed as an unsigned integer.x — the argument is treated as an integer and printed in hexadecimal representation (letters will be lowercase).X — the argument is treated as an integer and printed in hexadecimal representation (letters will be uppercase).The
cspecifier ignores the complement and width conversion specifiers. Attempting to use a combination of string and width specifiers with encodings that require more than one byte per character sometimes produces unexpected results.
Return value: string — formatted string.
Example of use:
str.format(“test: %s”, “Hello”)
// Returns: “test: Hello”
str.format(“%b, %x, %u”, 10, 20, 30)
// Returns: “1010, 14, 30”
codec.urlEncodeConverts a string to a format that is safe for transmission over the Internet. Encodes characters that are not in the allowed set in escape sequences according to RFC 3986.
An escape sequence is a way to write a character that cannot be inserted directly into a link.
For example, ! is replaced with %21. Thus, the string Hello! becomes %D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%21.
Signature: codec.urlEncode(str string) string
Arguments: str — the string to be encoded.
Result: A string in which all prohibited characters are replaced with escape sequences in the format %XX.
Example of use:
$encoded = codec.urlEncode(“Hello!”)
// $encoded will contain the string “%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%21”
codec.urlDecodeConverts a string encoded according to RFC 3986 to plain text.
Signature: codec.urlDecode(str string) string
Arguments: str — a string with special character codes (escape sequences).
Result: A plain string obtained after decoding.
Example of use:
$decoded = codec.urlDecode(“%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%21”)
// $decoded will contain the string “Hello!”
$decoded = codec.urlDecode(“%%”)
// $decoded will contain “%%”
str.splitSplits a string into parts using separator as a delimiter.
Signature: str.split(str string, separator string = “”, limit int = 0) List
Arguments:
str — the string to be split.separator — the separator. If an empty string is specified, the string is split into individual characters.limit — optional parameter equal to the maximum number of parts into which the string will be split:str.Result: a list of substrings into which the string was split.
Example of use:
$letters = str.split(“abcde”) // $letters will contain a list of letters of the word
$words = str.split(“one two three”, “ ”) // $words will contain a list of words
$words = str.split(“one two three”, “ ”, 2) // $words will contain [‘one’, “two three”]
$words = str.split(“one two three”, “ ”, -1) // $words will contain [‘one’, “two”]
str.firstReturns the string character at the specified position from the beginning of the string.
Signature: str.first(str string, index int = 0) string.
Arguments:
str - the string from which to get the character;index - an integer specifying the position of the character (starting from 0). Negative values are converted to positive values. The default is 0.Result: A string with one character corresponding to the position. If the position extends beyond the string, an empty string is returned.
Usage Example:
$str = "Word"
$firstLetter = str.first($str) // First letter
$secondLetter = str.first($str, 1) // Second letter
str.lastReturns the string character at the specified position from the end.
Signature: str.last(str string, index int = 0) string.
Arguments:
str - the string from which to get the character;index - an integer defining the position of the character from the end (starting from 0). Negative values are converted to positive values. The default is 0.Result: A string with one character corresponding to the position. If the position extends beyond the string, an empty string is returned.
Usage Example:
$str = "Word"
$lastLetter = str.last($str) // Last letter
$penultLetter = str.last($str, 1) // Penultimate letter
str.sub.Returns a portion of a string starting at the specified position and given length.
Signature: str.sub(str string, offset int, length int = nil) string.
Arguments:
str - source string;offset - position of the beginning of the substring:
length - length of the substring:
Result: A string containing the substring, or an empty string if the specified parameters extend beyond the boundaries of the original string.
Example Usage:
$sub = str.sub("abcdef", 1) // $sub equals bcdef
$sub = str.sub("abcdef", 1, 3) // $sub equals bcd
$sub = str.sub("abcdef", 0, 4) // $sub equals abcd
$sub = str. sub("abcdef", 0, 8) // $sub equals abcdef
$sub = str.sub("abcdef", -1, 1) // $sub equals f
$sub = str.sub("abcdef", -1) // $sub equals f
$sub = str. sub("abcdef", -2) // $sub is equal to ef
$sub = str.sub("abcdef", -3, 1) // $sub is equal to d
$sub = str.sub("abcdef", 0, -1) // $sub is equal to abcde
$sub = str. sub("abcdef", 2, -1) // $sub equals cde
$sub = str.sub("abcdef", 4, -4) // $sub equals empty string
$sub = str.sub("abcdef", -3, -1) // $sub equals de
str.join.joins elements of a list, tuple, or dictionary into a string with the specified delimiter.
Signature: str.join(arr Collection, separator string = "") string.
Arguments:
arr - the collection of elements to join (list, tuple or associative array);separator - separator string between elements. The default is an empty string.Result: A new string composed of the elements of the collection separated by the specified string.
Usage Example:
$str = str.join([1, 2, 3, 4, 5], "-") // $str will contain "1-2-3-4-5"
$str = str.join((("a", "b", "c"))) // $str will contain "abc"
$str = str.join({"a": "one", "b": "two"}, " + ") // $str will contain "one + two"
$str = str.join(["one"], "/") // $str will contain "one"
$str = str.join([], "/") // $str will contain ""
str.replace.Searches for all occurrences of a substring in a string and replaces them with the specified string.
Signature: str.replace(str string, search string, replace string) string.
Arguments:
str - the original string in which the replacement is performed;search - substring to be replaced;replace - string to replace the found occurrences with.Result: A new string with all occurrences of search replaced by replace.
Usage Example:
$str = str.replace("mom washed frame", "frame", "an") // $str contains "mom washed anna"
str.rreplace.Searches for all occurrences matching a regular expression and replaces them with the given string.
Signature: str.rreplace(str string, pattern string, replace string) string.
Arguments:
str - the string in which the replacement is performed;pattern - regular expression specifying the search pattern;replace - string to which the found matches will be replaced.Result: A new string with all pattern matches replaced by replace.
Regular expressions are used, Perl Compatible (PCRE).
Usage Example:
$str = str.rreplace("one 1, two 2, three 3", "/[0-9]/", "") // $str contains "one, two, three"
str.match.Checks if the string matches the given regular expression.
Signature: str.match(str string, pattern string) bool.
Arguments:
str - the string to check;pattern - regular expression to match.Result: Returns true if the string matches the expression, and false if it does not.
Regular expressions compatible with Perl (PCRE) are used.
Usage Example:
$isIntNumber = str.match("1.234", "/^[0-9]+$/") // $isIntNumber will be false
$isIntNumber = str.match("1234", "/^[0-9]+$/") // $isIntNumber will be true
str.distance.Calculates the degree of similarity between two strings as a number between 0 and 1.
Signature: str.distance(str1 string, str2 string) number.
Arguments:
str1 is the first string to compare;str2 is the second string to compare.Result: A number between 0 and 1:
The function uses the Word Error Rate (WER) metric.
Usage Example:
$d = str.distance("", "abc") // $d equals 0
$d = str.distance("Yes", "yes") // $d equals 1
$d = str.distance("trough", "opened") // $d equals 0.571
$d = str.distance("Yes, that's right", "tavern") // $d equals 0.625
$d = str.distance("creepy crooked bananas", "chew coconuts, eat bananas") // $d equals 0.714
$d = str.distance("went mad from the wound", "off he went to the infidels") // $d equals 0.45
$d = str.distance("hedgehog", "deoxyribonucleic acid") // $d equals 0
``''