Skip to contents

Convert strings to "UPPER CASE", "lower case", "snake_case" or "camelCase".

Usage

str_to_upper_case(strings)

str_to_lower_case(strings)

str_to_snake_case(strings, split_on_capital = FALSE)

str_to_camel_case(strings, capitalise_first = FALSE)

Arguments

strings

A character vector, where each element of the vector is a character string.

split_on_capital

str_to_snake_case() only: whether to treat every upper case letter as the start of a new word. This is for converting from camelCase.

capitalise_first

str_to_camel_case() only: whether the first letter of the first word in each string should be capitalised as in "PascalCase" not "camelCase".

Value

str_to_upper_case(): Returns strings with all lower case letters converted to upper case.

str_to_lower_case()

Returns strings with all upper case letters converted to lower case.

str_to_snake_case()

Returns strings with all upper case letters converted to lower case, non-alphanumeric characters between words converted to an underscore "_", and all preceding/trailing non-alphanumeric characters removed.

See also

str_replace_all() for replacing specific characters with other characters.

Base R tolower() and toupper().

Examples

string <- "A good day for kite-flying!"

str_to_upper_case(string)
#> [1] "A GOOD DAY FOR KITE-FLYING!"
str_to_lower_case(string)
#> [1] "a good day for kite-flying!"
str_to_snake_case(string)
#> [1] "a_good_day_for_kite_flying"
str_to_camel_case(string)
#> [1] "aGoodDayForKiteFlying"

# Optionally convert from camelCase:
str_to_snake_case(
  str_to_camel_case(string),
  split_on_capital = TRUE
)
#> [1] "a_good_day_for_kite_flying"

# Optionally capitalise first letter of the word
str_to_camel_case(
  string,
  capitalise_first = TRUE
)
#> [1] "AGoodDayForKiteFlying"