String Utilities
reverseString
Reverses the characters in a string
Usage
import { reverseString } from '@/lib/reverse-string';
const result = reverseString("TypeScript");
console.log(result); // "tpircSpeyT"Installation
pnpm dlx shadcn@latest add https://utilcn.dev/r/reverse-string.jsonbunx --bun shadcn@latest add https://utilcn.dev/r/reverse-string.jsonnpx shadcn@latest add https://utilcn.dev/r/reverse-string.jsonyarn shadcn@latest add https://utilcn.dev/r/reverse-string.jsonParameters
| Parameter | Type | Description |
|---|---|---|
str | string | The input string |
Returns
string - The reversed string.
Examples
reverseString("TypeScript"); // "tpircSpeyT"
reverseString("Hello World"); // "dlroW olleH"
reverseString("12345"); // "54321"
reverseString("a"); // "a"
reverseString(""); // ""Implementation
export function reverseString(str: string): string {
return str.split('').reverse().join('');
}Performance Note
This implementation is simple and readable. For very large strings, you might consider other approaches, but for typical use cases, this method is efficient and clear.