add functions: prefix? suffix?

This commit is contained in:
Pekka Laiho 2020-10-17 16:11:33 +07:00
parent 72dc10f369
commit 6475c5a253
2 changed files with 10 additions and 0 deletions

View File

@ -275,6 +275,8 @@ replace | `(replace "hello world" "hello" "bye")` | `"bye world"` | Replace subs
split | `(split "-" "a-b-c")` | `("a" "b" "c")` | Split string using [explode](https://www.php.net/manual/en/function.explode.php).
join | `(join "-" "a" "b" "c")` | `"a-b-c"` | Join string together using [implode](https://www.php.net/manual/en/function.implode.php).
format | `(format "%d %.2f" 56 4.5)` | `"56 4.50"` | Format string using [sprintf](https://www.php.net/manual/en/function.sprintf.php).
prefix? | `(prefix? "hello world" "hello")` | `true` | Return true if the first argument starts with the second argument.
suffix? | `(suffix? "hello world" "world")` | `true` | Return true if the first argument ends with the second argument.
Note that support for multibyte characters in strings is limited because the provided functions do not use the [mbstring](https://www.php.net/manual/en/book.mbstring.php) extension.

View File

@ -48,5 +48,13 @@ class Strings implements ILib
$env->set('format', new CoreFunc('format', 'Format the remaining arguments as string specified by the first argument.', 1, -1,
fn (string $a, ...$b) => sprintf($a, ...$b)
));
$env->set('prefix?', new CoreFunc('prefix?', 'Return true if the first argument starts with the second argument.', 2, 2,
fn (string $str, string $start) => substr($str, 0, strlen($start)) === $start
));
$env->set('suffix?', new CoreFunc('suffix?', 'Return true if the first argument ends with the second argument.', 2, 2,
fn (string $str, string $end) => substr($str, strlen($str) - strlen($end)) === $end
));
}
}