Truncate String
utilcn/truncate-stringFreeUtility
Truncate a string to a given length.
Install it
npx shadcn@latest add https://utilcn.dev/r/truncate-string.jsonSource
1/**2 * Truncates a string to a given length and appends an ellipsis ("...") if necessary.3 *4 * @param str - The input string.5 * @param length - The maximum length of the string.6 * @returns The truncated string with "..." if it exceeded the limit.7 *8 * @example9 * truncate("Hello TypeScript World", 10); // "Hello Type..."10 */11export function truncateString(str: string, length: number): string {12 return str.length > length ? `${str.slice(0, length)}...` : str;13}
Files it writes
More from utilcn
All 17 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Capitalizecapitalize | utilcn | Utilities | Free | no deps | |
| Capitalize Allcapitalize-all | utilcn | Utilities | Free | no deps | |
| Environment Server Configurationenv-server | utilcn | Utilities | Free | 1 dep | |
| Generate Presigned Download URLgenerate-presigned-download-url | utilcn | Utilities | Free | 2 deps · 2 files | |
| Generate Presigned Upload URLgenerate-presigned-upload-url | utilcn | Utilities | Free | 2 deps · 2 files | |
| Repeat Padrepeat-pad | utilcn | Utilities | Free | no deps | |
| Reverse Stringreverse-string | utilcn | Utilities | Free | no deps | |
| To Camel Caseto-camel-case | utilcn | Utilities | Free | no deps |
