Strings
Operators and functions for working with strings (str type) are described here.

Operator
Result
Description
str + str
str
Merges two strings.
* str
int
Returns the length of the string.
str ?
bool
Calls bool(str).
| str
str
This unary operator trims whitespace characters in the each line of the string.
str == str
bool
Returns true if the two strings are equal and false, otherwise.
str > str
bool
Returns true if the first string is greater than the second and false, otherwise.
str < str
bool
Returns true if the first string is less than the second and false, otherwise.
str != str
bool
Returns true if the two strings are not equal and false, otherwise.
str >= str
bool
Returns true if the first string is greater than or equal to the second and false, otherwise.
str <= str
bool
Returns true if the first line is less than or equal to the second and false, otherwise.
str = str
str
Assignment operator.
str = int
str
Converts an integer to a string and assigns it to a variable.
str = bool
str
Assigns "true" or "false".
str += str
str
Appends a string to a string variable.
str [ int ]
int
Sets/gets Unicode code by index.

The bool function returns false if the string is empty, "0" or "false", otherwise, it returns true.

The float function converts a string to a number of float type. If the string has the wrong format, an error is returned.

The int function converts a string to an integer number. If the string has the wrong format, an error is returned.

The Find function returns the index of the first instance of substr in s, or -1 if substr is not present in s.

The Format function formats according to a s specifier and returns the resulting string. There are the following format verbs:

  • %v - the value in a default format
  • %% - a literal percent sign; consumes no value

  • %t - the word true or false

  • %b - base 2
  • %c - the character represented by the corresponding Unicode code point
  • %d - base 10. This is the default format for int.
  • %o - base 8
  • %x - base 16, with lower-case letters for a-f
  • %X - base 16, with upper-case letters for A-F
  • %U - Unicode format: U+1234

  • %e - scientific notation, e.g. -1.234456e+78
  • %E - scientific notation, e.g. -1.234456E+78
  • %f - decimal point but no exponent, e.g. 123.456. You can specify the width and the precision like %[width].[precision]f - %8.2f, %.3f, %7f.
  • %g - %e for large exponents, %f otherwise. Precision is discussed below. This is the default format for float.

  • %s - the default format for strings
  • %x - base 16, lower-case, two characters per byte
  • %X - base 16, upper-case, two characters per byte
You can specify i-th argument in the format string like this - Format("%d %[1]d %[1]d", 10)
arr.int mya = {1,2,3}
time t
Format(`%s %v %v %g %6.2[4]f`, `ok`, mya, Now(t), 99.0 + 1.)

The HasPrefix function returns true if the string s begins with prefix.

The HasSuffix function returns true if the string s ends with suffix.

The Left function extracts a given number of characters from the left side of the string s.

The Lines function slices s multi-line string into all substrings separated by new line. All substrings are returned as an array of strings.

The Lower function converts a copy of s string to lower case and returns it.

The Repeat function returns a new string consisting of count copies of the string s.

The Replace function returns a copy of the string s with all old strings replaced by new string.

The Right function returns a substring of the last i characters of the s string.

The Size function returns the rounded size as a string. In the format parameter specify an output pattern for a decimal floating-point number and a string. If format is an empty string, the format %.2f%s is used.
Print( Size(956348901, `%.1f %s `) + Size(62, `%[2]s%.2[1]f `) + Size(123789, ``))
// 912.0 MB B62 120.89KB

The Split function slices s string into all substrings separated by sep string. All substrings are returned as an array of strings.

The Substr function returns a substring of the string s with the specified offset and length.

The Trim function returns a substring of the string s, with all leading and trailing Unicode code points contained in cutset removed.

The TrimLeft function returns a substring of the string s, with all leading Unicode code points contained in cutset removed.

The TrimRight function returns a substring of the string s, with all trailing Unicode code points contained in cutset removed.

The TrimSpace function returns a substring of the string s, with all leading and trailing white space removed.

The Upper function converts a copy of s string to upper case and returns it.
Last modified 1yr ago
Copy link
On this page
Operators
Functions
bool(str s) bool
float(str s) float
int(str s) int
Find(str s, str substr) int
Format(str s, anytype args...) str
HasPrefix(str s, str prefix) bool
HasSuffix(str s, str suffix) bool
Left(str s, int i) str
Lines(str s) arr.str
Lower(str s) str
Repeat(str s, int count) str
Replace(str s, str old, str new) str
Right(str s, int i) str
Size(int size, str format) str
Split(str s, str sep) arr.str
Substr(str s, int off, int length) str
Trim(str s, str cutset) str
TrimLeft(str s, str cutset) str
TrimRight(str s, str cutset) str
TrimSpace(str s) str
Upper(str s) str