String methods

Byte-level methods carried by every string: slice, at, indexOf — plus the chr() global for building strings from byte values.

Every mJS string carries these built-in methods. Remember that mJS strings are byte strings (see The mJS Language): indices count bytes, not Unicode characters, and strings may hold arbitrary binary data.

Functions

string.length

string.lenght -> number

Get the length of a string

Returns

number string length (bytes)

Example

let a = "Hello World";

print( a.length ); // 11

string.number()

string.number() -> number

Converts a string to a numeric value — the inverse of `number.toFixed()` and `string()`. (Global function.)

Converts a text string into a number type variable Wrapper to the strtod c function.

Returns

number Number value of string

Example

let text = "123.456";
let number = text.number();
print( number ); // 123.456

string.slice()

'some_string'.slice(start, end) → string

Returns a substring between two indices.

Arguments

NameTypeRequiredDefaultDescription
start number yes Start index (inclusive).
end number yes End index (exclusive).

Returns

string The substring.

Example

'abcdef'.slice(1,3) === 'bc';

string.at()

'abc'.at(index) → number

Returns the numeric byte value at a string index.

Arguments

NameTypeRequiredDefaultDescription
index number yes Byte index into the string.

Returns

number The byte value at that index.

Example

'abc'.at(0) === 0x61;

string.indexOf()

'abc'.indexOf(substr, [fromIndex]) → number

Returns the index of the first occurrence of a substring, or −1 if not found.

Arguments

NameTypeRequiredDefaultDescription
substr string yes Substring to search for.
fromIndex number optional 0 Index to start searching from.

Returns

number Index of the first occurrence, or −1 if not found.

Example

'abc'.indexOf('bc') === 1;

chr()

chr(n) → string | null

Returns a 1-byte string whose byte value is n — the inverse of string.at(). (Global function.)

Builds a single-byte string from a numeric value. If n is not numeric or is outside the 0–255 range, null is returned.

Arguments

NameTypeRequiredDefaultDescription
n number yes Byte value, 0–255.

Returns

string | null A 1-byte string, or null for invalid input.

Example

chr(0x61) === 'a';