@rbxts/jsnatives
at
Returns the character at a specified index in a string.
Signature
function at(str: string, index: NumericAdmissible): string
Description
The at
function returns the character at a specified position in a string. This method allows for both positive and negative integers, where negative integers count back from the end of the string.
Parameters
str
: The string to extract a character from.index
: The position of the character to get. If a string is provided, it will be converted to a number.
Return value
- A string containing a single character at the specified index.
- An empty string if the index is out of range.
Examples
Basic usage
// Get a character by indexconst char = String.at("Hello", 1);print(char); // Outputs: "e"
const firstChar = String.at("Hello", 0);print(firstChar); // Outputs: "H"
Using negative indices
// Negative indices count from the end of the stringconst str = "Hello";const lastChar = String.at(str, -1);print(lastChar); // Outputs: "o"
const secondToLast = String.at(str, -2);print(secondToLast); // Outputs: "l"
Using string indices
// String indices are converted to numbersconst str = "Hello";const char = String.at(str, "2");print(char); // Outputs: "l"
Handling out of range indices
const str = "Hello";
// Index beyond the string lengthconst outOfRange = String.at(str, 10);print(outOfRange); // Outputs: "" (empty string)
// Negative index beyond start of stringconst tooNegative = String.at(str, -10);print(tooNegative); // Outputs: "" (empty string)
Common use cases
// Get first characterfunction getFirstChar(str: string): string { return String.at(str, 0);}print(getFirstChar("Hello")); // Outputs: "H"
// Get last characterfunction getLastChar(str: string): string { return String.at(str, -1);}print(getLastChar("Hello")); // Outputs: "o"
// Safely get character at indexfunction safeCharAt(str: string, index: number): string | undefined { const char = String.at(str, index); return char !== "" ? char : undefined;}