Core\Str
Functions for text: measure it, search it, cut it, change its case and build new strings. Every position and length counts characters the way a person sees them, so an accented letter or an emoji counts as one.
Every member of Core\Str is a static function over a string that answers a new value; none changes its argument. Every position, length and count is in graphemes — user-perceived characters — so length("👍🏽") is 1, no slice or chunk ever splits a character, and a byte count is Core\Bytes::length. A string has no $s[0] and no </>: Core\Str::at, Core\Str::slice and Core\Str::compare are those spellings. A position or a cut that does not exist is null — indexOf, lastIndexOf, before, after — never false. In the same way, afterPrefix and beforeSuffix remove a given start or end and return the rest, or null when the string does not start or end with it.
<?nvsstring $s = Core\Str::trim(' Grüße aus Köln ');echo Core\Str::length($s), ' ', Core\Str::upper($s), "\n";echo Core\Str::slice($s, 0, 5), '|', Core\Str::at($s, 2), '|', Core\Str::reverse('café'), "\n";echo Core\Str::indexOf($s, 'aus') ?? 'none', ' ', Core\Str::indexOf($s, 'Bonn') ?? 'none', "\n";echo Core\Str::contains($s, 'Köln') ? 'yes' : 'no', ' ', Core\Str::countOf($s, 'ü'), "\n";array<string> $words = Core\Str::split($s, ' ');echo Core\Arr::count($words), ' ', Core\Str::join(Core\Arr::reverse($words), '-'), "\n";echo Core\Str::replace($s, 'Köln', 'Bonn'), '|', Core\Str::before($s, ' ') ?? 'none', "\n";echo Core\Str::padStart('7', 3, '0'), ' ', Core\Str::repeat('ab', 3), ' ', Core\Str::upperFirst('ßig'), "\n";echo Core\Str::format('%-6s|%05.1f|%x', 'id', 3.14159, 255), "\n";echo Core\Str::compare('img10', 'img9'), ' ', Core\Str::compare('img10', 'img9', { natural: true }), "\n";14 GRÜSSE AUS KÖLNGrüße|ü|éfac6 noneyes 13 Köln-aus-GrüßeGrüße aus Bonn|Grüße007 ababab SSigid |003.1|ff-1 1Members
42 members.
| Member | Signature |
|---|---|
length | length(string $s): uint |
at | at(string $s, int $index): string |
isEmpty | isEmpty(string $s): bool |
contains | contains(string $haystack, string $needle): bool |
startsWith | startsWith(string $s, string $prefix): bool |
endsWith | endsWith(string $s, string $suffix): bool |
slice | slice(string $s, int $offset, ?int $length = null): string |
indexOf | indexOf(string $haystack, string $needle, { from?: int, caseInsensitive?: bool }): ?uint |
lastIndexOf | lastIndexOf(string $haystack, string $needle, { before?: int, caseInsensitive?: bool }): ?uint |
countOf | countOf(string $haystack, string $needle): uint |
compare | compare(string $a, string $b, { caseInsensitive?: bool, natural?: bool }): int |
before | before(string $s, string $needle, { last?: bool }): ?string |
after | after(string $s, string $needle, { last?: bool }): ?string |
afterPrefix | afterPrefix(string $s, string $prefix): ?string |
beforeSuffix | beforeSuffix(string $s, string $suffix): ?string |
join | join(array<string> $parts, string $separator = ""): string |
split | split(string $s, string $separator, { limit?: int }): array<string> |
parts | parts(string<K> $text): array<string|K> |
chunk | chunk(string $s, uint $size): array<string> |
lines | lines(string $s): array<string> |
graphemes | graphemes(string $s): array<string> |
codePoints | codePoints(string $s): array<uint> |
replace | replace(string $s, string $search, string $replacement, { caseInsensitive?: bool, limit?: uint }): string |
replaceAll | replaceAll(string $s, array<string> $pairs, { caseInsensitive?: bool }): string |
replaceRange | replaceRange(string $s, int $offset, ?int $length, string $replacement): string |
padStart | padStart(string $s, uint $length, string $padding = " "): string |
padEnd | padEnd(string $s, uint $length, string $padding = " "): string |
trim | trim(string $s, { characters?: string }): string |
trimStart | trimStart(string $s, { characters?: string }): string |
trimEnd | trimEnd(string $s, { characters?: string }): string |
repeat | repeat(string $s, uint $times): string |
reverse | reverse(string $s): string |
wrap | wrap(string $s, uint $width, { breakWith?: string, cutLongWords?: bool }): string |
lower | lower(string $s): string |
upper | upper(string $s): string |
upperFirst | upperFirst(string $s): string |
lowerFirst | lowerFirst(string $s): string |
fold | fold(string $s): string |
normalize | normalize(string $s, Core\Str::NormalForm $form): string |
fromCodePoint | fromCodePoint(uint $codePoint): string |
fromCodePoints | fromCodePoints(array<uint> $codePoints): string |
format | format(string $template, mixed ...$arguments): string |
Enums
NormalFormThe four Unicode normal forms (UAX #15) thatCore\Str::normalizecan change text into. A form is either composed or decomposed, and either canonical or compatibility.
