Built-in methods are methods provided by Expr value types.
This page covers common value methods, number methods, and string methods.
It does not list methods of built-in objects such as arrays, maps, timers, serial ports, images, crypto objects, GUI controls, or user-defined classes. Those belong to object-specific reference pages.
A method is called on a value with dot syntax.
value.method(arguments);
Examples:
(123).to_string(); '123'.parse_num(); none().is_none();
Parentheses are useful when calling a method on a numeric literal.
(42).to_hex();
These methods can be called on any Expr value.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `is_none()` | none | number | True when the value is `none()`. |
| `is_nan()` | none | number | True when the value is numeric NaN. |
| `is_num()` | none | number | True when the value is a number, including NaN. |
| `is_num_notnan()` | none | number | True when the value is a number and not NaN. |
| `is_num_int()` | none | number | True when the value is an integer numeric value. |
| `is_string()` | none | number | True when the value is a string. |
| `is_bool()` | none | number | True when the value can be interpreted as a boolean value. |
Examples:
none().is_none(); // true nan().is_nan(); // true (42).is_num_int(); // true 'abc'.is_string(); // true
`is_bool()` is true for finite numbers and for strings that parse as `true`, `false`, `1`, or `0`.
These methods can be called on number values.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `to_bool()` | none | number | Converts the number to a boolean-style numeric value. |
| `to_int()` | none | number | Converts to signed 32-bit integer. |
| `to_uint()` | none | number | Converts to unsigned 32-bit integer. |
| `to_s8()` | none | number | Converts to signed 8-bit integer. |
| `to_u8()` | none | number | Converts to unsigned 8-bit integer. |
| `to_s16()` | none | number | Converts to signed 16-bit integer. |
| `to_u16()` | none | number | Converts to unsigned 16-bit integer. |
| `to_s32()` | none | number | Converts to signed 32-bit integer. |
| `to_u32()` | none | number | Converts to unsigned 32-bit integer. |
| `to_s64()` | none | number | Converts to signed 64-bit integer. |
| `to_u64()` | none | number | Converts to unsigned 64-bit integer. |
| `to_string()` | none | string | Converts to decimal string. |
| `to_string(format)` | string | string | Converts to string using a format string. |
| `to_hex()` | none | string | Converts to hexadecimal string. |
| `to_bin()` | none | string | Converts to binary string. |
| `chr()` | none | string | Converts the low byte of the number to one character. |
| `chr(count)` | number | string | Repeats that character `count` times. |
Examples:
(255).to_hex(); // FF (5).to_bin(); // binary text (65).chr(); // 'A' (123.456).to_string();
Integer conversion methods round according to Expr numeric conversion rules.
`bit()` reads or changes one bit in an integer-like number.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `bit(index)` | number | number | Returns true when bit `index` is set. |
| `bit(index, value)` | number, value | number | Returns a copy of the number with bit `index` set or cleared. |
The bit index must be an integer from `0` to `63`.
Examples:
(5).bit(0); // true (5).bit(1); // false (4).bit(0, true); // 5 (5).bit(2, false); // 1
These methods can be called on string values.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `length()` | none | number | Number of characters in the string. |
| `to_string()` | none | string | Returns the string value. |
| `contains(text)` | string | number | True when the string contains `text`. |
| `startswith(prefix)` | string | number | True when the string starts with `prefix`. |
| `endswith(suffix)` | string | number | True when the string ends with `suffix`. |
| `indexof(text)` | string | number | First position of `text`, or `-1` when not found. |
| `reverse()` | none | string | Reversed string. |
Examples:
'PlanetCNC'.length(); 'PlanetCNC'.contains('CNC'); 'abc'.reverse(); // 'cba'
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `substr(start)` | number | string | Substring from `start` to the end. |
| `substr(start, count)` | number, number | string | Substring starting at `start` with `count` characters. |
| `slice(start)` | number | string | Alias for `substr(start)`. |
| `slice(start, count)` | number, number | string | Alias for `substr(start, count)`. |
| `subspan(start)` | number | string | Substring from `start` to the end. |
| `subspan(start, end)` | number, number | string | Substring from `start` to `end`. |
| `span(start)` | number | string | Alias for `subspan(start)`. |
| `span(start, end)` | number, number | string | Alias for `subspan(start, end)`. |
| `before(text)` | string | string | Text before the first matching separator. |
| `after(text)` | string | string | Text after the first matching separator. |
Examples:
'abcdef'.substr(2, 3); // 'cde' 'key=value'.before('='); // 'key' 'key=value'.after('='); // 'value'
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `parse_num()` | none | number | Parses decimal numeric text. |
| `parse_hex()` | none | number | Parses hexadecimal text. |
| `parse_bin()` | none | number | Parses binary text. |
| `parse_bool()` | none | number | Parses boolean text. |
Examples:
'123'.parse_num(); 'FF'.parse_hex(); '1010'.parse_bin(); 'true'.parse_bool();
Parsing methods raise a runtime error when the string cannot be parsed by the requested method.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `repeat(count)` | number | string | Repeats the string `count` times. |
| `padleft(pad, length)` | string, number | string | Pads on the left until target length is reached. |
| `padright(pad, length)` | string, number | string | Pads on the right until target length is reached. |
| `table()` | none | string | Formats semicolon-separated text as a table. |
| `table(header)` | value | string | Formats semicolon-separated text as a table, optionally treating the first row as a header. |
| `ord()` | none | number | Numeric code of the first character, or `0` for an empty string. |
Examples:
'ab'.repeat(3); // 'ababab' '7'.padleft('0', 3); // '007' 'A'.ord(); // 65
These methods use regular-expression patterns.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `match(pattern)` | string | number | True when the pattern matches. |
| `find(pattern)` | string | string | First matching text, or empty string when there is no match. |
| `replace(pattern, replacement)` | string, string | string | Replaces all matches with replacement text. |
Examples:
'abc123'.match('[0-9]+'); 'abc123'.find('[0-9]+'); 'abc123'.replace('[0-9]+', '');
Invalid regular-expression patterns raise a runtime error.
| Method | Arguments | Returns | Meaning |
|---|---|---|---|
| `trim()` | none | string | Removes whitespace from both ends. |
| `trimleft()` | none | string | Removes whitespace from the start. |
| `trimright()` | none | string | Removes whitespace from the end. |
| `upper()` | none | string | Converts to uppercase. |
| `lower()` | none | string | Converts to lowercase. |
Examples:
' abc '.trim(); 'abc'.upper(); 'ABC'.lower();
Object methods are not listed on this page.
Examples of object methods that belong to object-specific pages:
array(1, 2, 3).size(); map().set('x', 1); window().show(); serial().open('COM1');
Those methods are part of the object returned by the constructor, not part of the built-in value-method set.
Use parentheses when the target is a numeric literal.
(123).to_string();
Conversions are methods on values.
value.to_string(); text.parse_num();
Do not use old global conversion-function style.
`array().size()` is a method, but it is an array object method. It is not documented on this page.
Previous: Methods and properties
Next: Common Value Methods