← Back to utilities
string

capitalize

Capitalizes the first letter of a string. Converts the first character to uppercase while leaving the rest unchanged. Safely handles edge cases like empty strings and non-string inputs.

Installation

npx fragmen add string/capitalize

Source Code

/**
 * Capitalizes the first letter of a string.
 *
 * Converts the first character to uppercase while leaving the rest unchanged.
 * Safely handles edge cases like empty strings and non-string inputs.
 *
 * @tags pure, string-manipulation, formatting
 * @param {string} str The string to capitalize.
 * @returns {string} The string with the first letter capitalized, or empty string if invalid input.
 *
 * @example
 * ```typescript
 * capitalize('hello world');  // 'Hello world'
 * capitalize('javaScript');   // 'JavaScript'
 * capitalize('HTML');         // 'HTML'
 * capitalize('');             // ''
 * capitalize('a');            // 'A'
 * ```
 */
export function capitalize(str: string): string {
  if (typeof str !== 'string' || str.length === 0) {
    return '';
  }

  return str.charAt(0).toUpperCase() + str.slice(1);
}

Examples

capitalize('hello world');  // 'Hello world'
capitalize('javaScript');   // 'JavaScript'
capitalize('HTML');         // 'HTML'
capitalize('');             // ''
capitalize('a');            // 'A'

Related Utilities

camel-case

string

Converts a string to camelCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word except the first one. Commonly used for JavaScript variable names, object properties, and function names.

#pure#string-manipulation#formatting

kebab-case

string

Converts a string to kebab-case. Transforms a string by converting it to lowercase and replacing spaces, underscores, and camelCase boundaries with hyphens. Useful for creating URL slugs, CSS class names, and file names.

#pure#string-manipulation#formatting

pad-end

string

Pads the end of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pad-start

string

Pads the start of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pascal-case

string

Converts a string to PascalCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word including the first one. Commonly used for class names, interface names, type names, and component names.

#pure#string-manipulation#formatting

snake-case

string

Converts a string to snake_case. Transforms a string by converting it to lowercase and replacing spaces, hyphens, and camelCase boundaries with underscores. Commonly used for database column names, Python variables, and configuration keys.

#pure#string-manipulation#formatting

Quick Actions

Estimated size:834 B

Tags

Parameters

strstring

The string to capitalize.

Returns

string

The string with the first letter capitalized, or empty string if invalid input.