Core\Str::trimStart
Core\Str::trimStart(string $s, {characters?: string}): stringCore\Str::trimStart( string $s, {characters?: string}, ): string
Removes characters from the start of a string.
Core\Str::trimStart returns the string without the spaces, tabs and line breaks at its start. The end of the string is not changed. This is useful when the end of a text matters, for example the spaces that end a line of a fixed-width table.
The characters option gives your own list of characters to remove. It replaces the default list, so { characters: "0" } removes only zeros. Each character in the list counts on its own, and the order does not matter. When every character of the string is in the list, the result is the empty string.
A trim removes whole characters, as a person counts them. Some characters have several code points (Unicode numbers). Such a character is removed only when all its code points are in the list. "\r\n" is one character, so Core\Str::trimStart("\r\nHi", { characters: "\r" }) returns "\r\nHi" unchanged. The default list removes "\r\n".
Core\Str::trim removes characters from both ends, and Core\Str::trimEnd from the end only.
The examples below remove spaces at the start of a line, remove the zeros before a number, and read the headings of a Markdown document.
Parameters
$sstringThe string to trim.
Options
charactersstringoptionalThe characters to remove, in any order.a..zis the three charactersa,.andz. The default is space, tab, newline, carriage return, NUL and vertical tab. A character made of several code points, such asewith an accent mark, is removed only when all of its code points are in this set.
Return value
Returns string. $s without those characters at its start. The rest of $s is not changed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Str::trim, Core\Str::trimStart and Core\Str::trimEnd remove whole characters. A character at the end is removed only when the characters option has every code point of it. So Core\Str::trim("e\u{301}", { characters: "\u{301}" }) returns the text unchanged, because the accent belongs to the e. |
Examples
Remove spaces at the start
<?nvs// Shows how to remove the spaces at the start of a string and keep the ones at its end.
string $line = ' return 42; ';
// Only the spaces at the start are removed. This prints `[return 42; ]`.echo '[', Core\Str::trimStart($line), "]\n";
// Tabs and line breaks are removed too. This prints `[done]`.echo '[', Core\Str::trimStart("\n\t done"), "]\n";[return 42; ][done]Remove leading zeros
<?nvs// Shows how to remove the zeros before a number, such as an order number.
array<string> $numbers = ['000042', '1050', '0000'];
foreach ($numbers as string $number) { string $short = Core\Str::trimStart($number, { characters: '0' }); // "0000" has only zeros, so the result is the empty string. It is printed as `0`. if ($short == '') { $short = '0'; } // This prints `42`, `1050` and `0`. echo $short, "\n";}4210500Read markdown headings
<?nvs// Shows how to read the headings of a Markdown document, and how deep each one is.
string $document = "# Shop\nWelcome!\n## Opening hours\nMonday to Friday\n### Holidays\n";
foreach (Core\Str::split($document, "\n") as string $line) { if (!Core\Str::startsWith($line, '#')) { continue; } // The `#` signs are removed first, then the space after them. string $title = Core\Str::trimStart(Core\Str::trimStart($line, { characters: '#' })); // The number of removed characters is the depth of the heading. uint $depth = Core\Str::length($line) - Core\Str::length($title) - 1; // This prints `1 Shop`, `2 Opening hours` and `3 Holidays`. echo $depth, ' ', $title, "\n";}1 Shop2 Opening hours3 Holidays