Function to convert strings to any case
to_any_case(string, case = c("snake", "small_camel", "big_camel", "screaming_snake", "parsed", "mixed", "lower_upper", "upper_lower", "swap", "all_caps", "lower_camel", "upper_camel", "internal_parsing", "none", "flip", "sentence", "random", "title"), abbreviations = NULL, sep_in = "[^[:alnum:]]", parsing_option = 1, transliterations = NULL, numerals = c("middle", "left", "right", "asis", "tight"), sep_out = NULL, unique_sep = NULL, empty_fill = NULL, prefix = "", postfix = "")
string | A string (for example names of a data frame). |
---|---|
case | The desired target case, provided as one of the following:
There are five "special" cases available:
|
abbreviations | character. (Case insensitive) matched abbreviations are surrounded by underscores. In this way, they can get recognized by the parser. This is useful when e.g. Use this feature with care: One letter abbreviations and abbreviations next to each other are hard to read and also not easy to parse for further processing. |
sep_in | (short for separator input) if character, is interpreted as a
regular expression (wrapped internally into |
parsing_option | An integer that will determine the parsing_option.
|
transliterations | A character vector (if not |
numerals | A character specifying the alignment of numerals ( |
sep_out | (short for separator output) String that will be used as separator. The defaults are |
unique_sep | A string. If not |
empty_fill | A string. If it is supplied, then each entry that matches "" will be replaced by the supplied string to this argument. |
prefix | prefix (string). |
postfix | postfix (string). |
A character vector according the specified parameters above.
to_any_case()
is vectorised over string
, sep_in
, sep_out
,
empty_fill
, prefix
and postfix
.
snakecase on github or
caseconverter
for some handy shortcuts.
#> [1] "hh_city" "new_us_elections"#> [1] "SuccesfullGmbH"#> [1] "Succesfull GmbH"### sep_in (input separator) string <- "R.St\u00FCdio: v.1.0.143" to_any_case(string)#> [1] "r_stüdio_v_1_0_143"to_any_case(string, sep_in = ":|\\.")#> [1] "r_stüdio_v_1_0_143"to_any_case(string, sep_in = ":|(?<!\\d)\\.")#> [1] "r_stüdio_v_1.0.143"### parsing_option # the default option makes no sense in this setting to_parsed_case("HAMBURGcity", parsing_option = 1)#> [1] "HAMBUR_Gcity"# so the second parsing option is the way to address this example to_parsed_case("HAMBURGcity", parsing_option = 2)#> [1] "HAMBURG_city"# By default (option 1) characters are converted after non alpha numeric characters. # To suppress this behaviour add a minus to the parsing_option to_upper_camel_case("lookBehindThe.dot", parsing_option = -1)#> [1] "LookBehindTheDot"# For some exotic cases parsing option 3 might be of interest to_parsed_case("PARSingOption3", parsing_option = 3)#> [1] "PARSing_Option_3"# There may be reasons to suppress the parsing to_any_case("HAMBURGcity", parsing_option = 0)#> [1] "hamburgcity"### transliterations to_any_case("\u00E4ngstlicher Has\u00EA", transliterations = c("german", "Latin-ASCII"))#> [1] "aengstlicher_hase"### case strings <- c("this Is a Strange_string", "AND THIS ANOTHER_One") to_any_case(strings, case = "snake")#> [1] "this_is_a_strange_string" "and_this_another_one"to_any_case(strings, case = "lower_camel") # same as "small_camel"#> [1] "thisIsAStrangeString" "andThisAnotherOne"to_any_case(strings, case = "upper_camel") # same as "big_camel"#> [1] "ThisIsAStrangeString" "AndThisAnotherOne"to_any_case(strings, case = "all_caps") # same as "screaming_snake"#> [1] "THIS_IS_A_STRANGE_STRING" "AND_THIS_ANOTHER_ONE"to_any_case(strings, case = "lower_upper")#> [1] "thisISaSTRANGEstring" "andTHISanotherONE"to_any_case(strings, case = "upper_lower")#> [1] "THISisAstrangeSTRING" "ANDthisANOTHERone"to_any_case(strings, case = "sentence")#> [1] "This is a strange string" "And this another one"to_any_case(strings, case = "title")#> [1] "This is a Strange String" "And this Another One"to_any_case(strings, case = "parsed")#> [1] "this_Is_a_Strange_string" "AND_THIS_ANOTHER_One"to_any_case(strings, case = "mixed")#> [1] "this_Is_a_Strange_string" "And_This_Another_One"to_any_case(strings, case = "swap")#> [1] "THIS iS A sTRANGE_STRING" "and this another_oNE"to_any_case(strings, case = "random")#> [1] "ThiS Is a StrAnGE_sTrIng" "And ThIs aNothER_oNE"to_any_case(strings, case = "none")#> [1] "this_Is_a_Strange_string" "AND_THIS_ANOTHER_One"to_any_case(strings, case = "internal_parsing")#> [1] "this_Is_a_Strange_string" "AND_THIS_ANOTHER_One"#> [1] "species42value_23month_7_8"#> [1] "species42_value23_month7_8"#> [1] "species_42value_23month_7_8"#> [1] "species_42_value_23_month_7_8"#> [1] "species42value23month7_8"### sep_out (output separator) string <- c("lowerCamelCase", "ALL_CAPS", "I-DontKNOWWhat_thisCASE_is") to_snake_case(string, sep_out = ".")#> [1] "lower.camel.case" "all.caps" #> [3] "i.dont.know.what.this.case.is"#> [1] "lower Camel Case" "All Caps" #> [3] "I Dont Know What this Case is"#> [1] "LOWER=CAMEL=CASE" "ALL=CAPS" #> [3] "I=DONT=KNOW=WHAT=THIS=CASE=IS"#> [1] "empty" "empty" "also empty"#> [1] "same" "same>1" "same>2" "other"### prefix and postfix to_upper_camel_case("some_path", sep_out = "//", prefix = "USER://", postfix = ".exe")#> [1] "USER://Some//Path.exe"