Skip to content

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.

<?nvs
string $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";
Output
14 GRÜSSE AUS KÖLN
Grüße|ü|éfac
6 none
yes 1
3 Köln-aus-Grüße
Grüße aus Bonn|Grüße
007 ababab SSig
id |003.1|ff
-1 1

Members

42 members.

MemberSignature
lengthlength(string $s): uint
atat(string $s, int $index): string
isEmptyisEmpty(string $s): bool
containscontains(string $haystack, string $needle): bool
startsWithstartsWith(string $s, string $prefix): bool
endsWithendsWith(string $s, string $suffix): bool
sliceslice(string $s, int $offset, ?int $length = null): string
indexOfindexOf(string $haystack, string $needle, { from?: int, caseInsensitive?: bool }): ?uint
lastIndexOflastIndexOf(string $haystack, string $needle, { before?: int, caseInsensitive?: bool }): ?uint
countOfcountOf(string $haystack, string $needle): uint
comparecompare(string $a, string $b, { caseInsensitive?: bool, natural?: bool }): int
beforebefore(string $s, string $needle, { last?: bool }): ?string
afterafter(string $s, string $needle, { last?: bool }): ?string
afterPrefixafterPrefix(string $s, string $prefix): ?string
beforeSuffixbeforeSuffix(string $s, string $suffix): ?string
joinjoin(array<string> $parts, string $separator = ""): string
splitsplit(string $s, string $separator, { limit?: int }): array<string>
partsparts(string<K> $text): array<string|K>
chunkchunk(string $s, uint $size): array<string>
lineslines(string $s): array<string>
graphemesgraphemes(string $s): array<string>
codePointscodePoints(string $s): array<uint>
replacereplace(string $s, string $search, string $replacement, { caseInsensitive?: bool, limit?: uint }): string
replaceAllreplaceAll(string $s, array<string> $pairs, { caseInsensitive?: bool }): string
replaceRangereplaceRange(string $s, int $offset, ?int $length, string $replacement): string
padStartpadStart(string $s, uint $length, string $padding = " "): string
padEndpadEnd(string $s, uint $length, string $padding = " "): string
trimtrim(string $s, { characters?: string }): string
trimStarttrimStart(string $s, { characters?: string }): string
trimEndtrimEnd(string $s, { characters?: string }): string
repeatrepeat(string $s, uint $times): string
reversereverse(string $s): string
wrapwrap(string $s, uint $width, { breakWith?: string, cutLongWords?: bool }): string
lowerlower(string $s): string
upperupper(string $s): string
upperFirstupperFirst(string $s): string
lowerFirstlowerFirst(string $s): string
foldfold(string $s): string
normalizenormalize(string $s, Core\Str::NormalForm $form): string
fromCodePointfromCodePoint(uint $codePoint): string
fromCodePointsfromCodePoints(array<uint> $codePoints): string
formatformat(string $template, mixed ...$arguments): string

Enums

  • NormalFormThe four Unicode normal forms (UAX #15) that Core\Str::normalize can change text into. A form is either composed or decomposed, and either canonical or compatibility.