Skip to content

Core\Str::at

Returns the one character at a position in a string.

Core\Str::at counts positions from 0, so position 0 is the first character. A negative position counts from the end: -1 is the last character and -2 the one before it. The result is always a string with exactly one character in it.

A character here is what a person sees as one letter or symbol. So "é" is one character, and so is an emoji built from several parts. This is the same way Core\Str::length counts, so a position from 0 up to the length minus one always finds a character.

A position outside the string throws a RuntimeError.

The examples below read the first and last character, catch a position that is too far, and build the initials of a person's name.

Parameters

  • $sstringThe string to read from.
  • $indexintThe position of the character. The first character is at 0. A negative position counts from the end, so -1 is the last character.

Return value

Returns string. A string with exactly one character in it.

Errors

  • RuntimeErrorThere is no character at $index. The position is at or past the length of $s, counting from either end.

Examples

First and last character

<?nvs
// Shows how to read one character of a string, from the start or from the end.
string $city = 'Zürich';
// Position `0` is the first character. This prints `Z`.
echo Core\Str::at($city, 0), "\n";
// `ü` is one character, so position 1 is `ü`. This prints `ü`.
echo Core\Str::at($city, 1), "\n";
// A negative position counts from the end. `-1` is the last character.
// This prints `h`.
echo Core\Str::at($city, -1), "\n";
// An emoji made of several parts is still one character. This prints `👍🏽`.
echo Core\Str::at('ok 👍🏽', 3), "\n";
Output
Z
ü
h
👍🏽

A position outside the string

<?nvs
// Shows what happens when the position is outside the string.
string $code = 'AB7';
// The string has 3 characters, so the last position is 2. This prints `7`.
echo Core\Str::at($code, 2), "\n";
// Position 3 is past the end, so `Core\Str::at` throws a `RuntimeError`.
// This prints the error message.
try {
echo Core\Str::at($code, 3), "\n";
} catch (RuntimeError $error) {
echo 'no character there: ', $error->message, "\n";
}
// An empty string has no characters at all, so position 0 throws too.
// Check the length first when the string can be empty. This prints `(empty)`.
string $name = '';
if (Core\Str::length($name) > 0) {
echo Core\Str::at($name, 0), "\n";
} else {
echo "(empty)\n";
}
Output
7
no character there: Core\Str::at(): index 3 is outside a string of 3 characters
(empty)

Initials for an avatar

<?nvs
// Shows how to build a person's initials, as a website does for the round
// picture next to a user's name.
array<string> $users = ['Ada Lovelace', 'émile zola', 'Grace'];
foreach ($users as string $user) {
string $initials = '';
foreach (Core\Str::split($user, ' ') as string $word) {
// Two spaces in a row give an empty word. It has no first character.
if (Core\Str::isEmpty($word)) {
continue;
}
$initials = $initials . Core\Str::upper(Core\Str::at($word, 0));
}
// This prints `AL`, then `ÉZ`, then `G`.
echo $user, ': ', $initials, "\n";
}
Output
Ada Lovelace: AL
émile zola: ÉZ
Grace: G