Skip to content

toSnakeCase

Converts a string to snake_case format (lowercase and words separated by underscores).

Syntax

typescript
toSnakeCase(str: string): string

Parameters

ParameterTypeDescription
strstringThe input string.

Returns

TypeDescription
stringThe string converted to snake_case.

Example

typescript
toSnakeCase("Exemplo de String"); // "exemplo_de_string"
toSnakeCase("  Teste Snake Case  "); // "teste_snake_case"
toSnakeCase(""); // ""

Notes

  • Throws TypeError if the argument is not a string.
  • Extra spaces are removed before conversion.

References

Released under the MIT License.