Miscellaneous string functions that don't quite fit into the other categories.

string_dup(s, times)
string_insert(s, insert, position)
string_split(s, split, ...)
string_len(s)
string_reverse(s)
string_unique(s)
string_sort(s, descend = FALSE)

Arguments

s

A string (character) vector.

times

The number of times to duplicate a string.

insert

The string to insert.

position

The position index to which to insert the string.

split

The character by which to split a string vector.

...

Parameters passed to strsplit().

descend

Whether to sort in descending order (TRUE) or not (FALSE; the default).

Details

string_dup() acts the same as strrep(): it repeats a string n number of times.

string_insert() inserts a string at a specified position.

string_len() acts the same as nchar(): it counts the number of characters per vector element.

string_reverse() reverses the characters in a string.

string_split() acts the same as strsplit(): it splits a string by a specified character.

string_unique() obtains the distinct characters for each element in a vector.

string_sort() arranges the letters in the character vector in order if descend = FALSE (the default) and reverse order if descend = TRUE.

The synonym pattern of these functions are s_*() and *() (insert, len, and reverse). For the former pattern, you can replace with split, unique, and sort, as well.

See also

Examples

string_dup(rownames(mtcars), 3)
#> [1] "Mazda RX4Mazda RX4Mazda RX4" #> [2] "Mazda RX4 WagMazda RX4 WagMazda RX4 Wag" #> [3] "Datsun 710Datsun 710Datsun 710" #> [4] "Hornet 4 DriveHornet 4 DriveHornet 4 Drive" #> [5] "Hornet SportaboutHornet SportaboutHornet Sportabout" #> [6] "ValiantValiantValiant" #> [7] "Duster 360Duster 360Duster 360" #> [8] "Merc 240DMerc 240DMerc 240D" #> [9] "Merc 230Merc 230Merc 230" #> [10] "Merc 280Merc 280Merc 280" #> [11] "Merc 280CMerc 280CMerc 280C" #> [12] "Merc 450SEMerc 450SEMerc 450SE" #> [13] "Merc 450SLMerc 450SLMerc 450SL" #> [14] "Merc 450SLCMerc 450SLCMerc 450SLC" #> [15] "Cadillac FleetwoodCadillac FleetwoodCadillac Fleetwood" #> [16] "Lincoln ContinentalLincoln ContinentalLincoln Continental" #> [17] "Chrysler ImperialChrysler ImperialChrysler Imperial" #> [18] "Fiat 128Fiat 128Fiat 128" #> [19] "Honda CivicHonda CivicHonda Civic" #> [20] "Toyota CorollaToyota CorollaToyota Corolla" #> [21] "Toyota CoronaToyota CoronaToyota Corona" #> [22] "Dodge ChallengerDodge ChallengerDodge Challenger" #> [23] "AMC JavelinAMC JavelinAMC Javelin" #> [24] "Camaro Z28Camaro Z28Camaro Z28" #> [25] "Pontiac FirebirdPontiac FirebirdPontiac Firebird" #> [26] "Fiat X1-9Fiat X1-9Fiat X1-9" #> [27] "Porsche 914-2Porsche 914-2Porsche 914-2" #> [28] "Lotus EuropaLotus EuropaLotus Europa" #> [29] "Ford Pantera LFord Pantera LFord Pantera L" #> [30] "Ferrari DinoFerrari DinoFerrari Dino" #> [31] "Maserati BoraMaserati BoraMaserati Bora" #> [32] "Volvo 142EVolvo 142EVolvo 142E"
string_insert("abcd", "Z", 3)
#> [1] "abZcd"
string_len(rownames(mtcars))
#> [1] 9 13 10 14 17 7 10 9 8 8 9 10 10 11 18 19 17 8 11 14 13 16 11 10 16 #> [26] 9 13 12 14 12 13 10
string_reverse(rownames(mtcars))
#> [1] "4XR adzaM" "gaW 4XR adzaM" "017 nustaD" #> [4] "evirD 4 tenroH" "tuobatropS tenroH" "tnailaV" #> [7] "063 retsuD" "D042 creM" "032 creM" #> [10] "082 creM" "C082 creM" "ES054 creM" #> [13] "LS054 creM" "CLS054 creM" "doowteelF callidaC" #> [16] "latnenitnoC nlocniL" "lairepmI relsyrhC" "821 taiF" #> [19] "civiC adnoH" "alloroC atoyoT" "anoroC atoyoT" #> [22] "regnellahC egdoD" "nilevaJ CMA" "82Z oramaC" #> [25] "driberiF caitnoP" "9-1X taiF" "2-419 ehcsroP" #> [28] "aporuE sutoL" "L aretnaP droF" "oniD irarreF" #> [31] "aroB itaresaM" "E241 ovloV"
string_split(rownames(mtcars), " ")
#> [[1]] #> [1] "Mazda" "RX4" #> #> [[2]] #> [1] "Mazda" "RX4" "Wag" #> #> [[3]] #> [1] "Datsun" "710" #> #> [[4]] #> [1] "Hornet" "4" "Drive" #> #> [[5]] #> [1] "Hornet" "Sportabout" #> #> [[6]] #> [1] "Valiant" #> #> [[7]] #> [1] "Duster" "360" #> #> [[8]] #> [1] "Merc" "240D" #> #> [[9]] #> [1] "Merc" "230" #> #> [[10]] #> [1] "Merc" "280" #> #> [[11]] #> [1] "Merc" "280C" #> #> [[12]] #> [1] "Merc" "450SE" #> #> [[13]] #> [1] "Merc" "450SL" #> #> [[14]] #> [1] "Merc" "450SLC" #> #> [[15]] #> [1] "Cadillac" "Fleetwood" #> #> [[16]] #> [1] "Lincoln" "Continental" #> #> [[17]] #> [1] "Chrysler" "Imperial" #> #> [[18]] #> [1] "Fiat" "128" #> #> [[19]] #> [1] "Honda" "Civic" #> #> [[20]] #> [1] "Toyota" "Corolla" #> #> [[21]] #> [1] "Toyota" "Corona" #> #> [[22]] #> [1] "Dodge" "Challenger" #> #> [[23]] #> [1] "AMC" "Javelin" #> #> [[24]] #> [1] "Camaro" "Z28" #> #> [[25]] #> [1] "Pontiac" "Firebird" #> #> [[26]] #> [1] "Fiat" "X1-9" #> #> [[27]] #> [1] "Porsche" "914-2" #> #> [[28]] #> [1] "Lotus" "Europa" #> #> [[29]] #> [1] "Ford" "Pantera" "L" #> #> [[30]] #> [1] "Ferrari" "Dino" #> #> [[31]] #> [1] "Maserati" "Bora" #> #> [[32]] #> [1] "Volvo" "142E" #>
string_unique(rownames(mtcars))
#> [1] "Mazd RX4" "Mazd RX4Wg" "Datsun 710" "Hornet 4Div" #> [5] "Hornet Spabu" "Valint" "Duster 360" "Merc 240D" #> [9] "Merc 230" "Merc 280" "Merc 280C" "Merc 450SE" #> [13] "Merc 450SL" "Merc 450SLC" "Cadilc Fetwo" "Lincol Ctea" #> [17] "Chrysle Impia" "Fiat 128" "Honda Civc" "Toyta Crl" #> [21] "Toyta Crn" "Dodge Chalnr" "AMC Javelin" "Camro Z28" #> [25] "Pontiac Frebd" "Fiat X1-9" "Porsche 914-2" "Lotus Erpa" #> [29] "Ford PanteL" "Ferai Dno" "Maserti Bo" "Volv 142E"
string_sort(rownames(mtcars))
#> [1] " 4aadMRXz" " 4aaadgMRWXz" " 017aDnstu" #> [4] " 4DeeHinorrtv" " abeHnoooprrStttu" "aailntV" #> [7] " 036Derstu" " 024cDeMr" " 023ceMr" #> [10] " 028ceMr" " 028cCeMr" " 045ceEMrS" #> [13] " 045ceLMrS" " 045cCeLMrS" " aacCddeeFilllootw" #> [16] " acCeiillLnnnnnoott" " aCeehiIllmprrrsy" " 128aFit" #> [19] " acCdHiinov" " aaClloooortTy" " aaCnoooortTy" #> [22] " aCdDeeegghllnor" " aACeiJlMnv" " 28aaCmorZ" #> [25] " abcdeFiiinoPrrt" "- 19aFitX" "- 1249cehoPrs" #> [28] " aELooprstuu" " aadeFLnoPrrt" " aDeFiinorrr" #> [31] " aaaBeiMorrst" " 124EloovV"
string_sort(rownames(mtcars), descend = TRUE)
#> [1] "zXRMdaa4 " "zXWRMgdaaa4 " "utsnDa710 " #> [4] "vtrroniHeeD4 " "utttSrrpooonHeba " "Vtnliaa" #> [7] "utsreD630 " "rMeDc420 " "rMec320 " #> [10] "rMec820 " "rMeCc820 " "SrMEec540 " #> [13] "SrMLec540 " "SrMLeCc540 " "wtoollliFeeddCcaa " #> [16] "ttoonnnnnLlliieCca " "ysrrrpmllIiheeCa " "tiFa821 " #> [19] "voniiHdCca " "yTtroooollCaa " "yTtroooonCaa " #> [22] "ronllhggeeeDdCa " "vnMlJieCAa " "ZromCaa82 " #> [25] "trrPoniiiFedcba " "XtiFa91 -" "srPohec9421 -" #> [28] "uutsrpooLEa " "trrPonLFedaa " "rrroniiFeDa " #> [31] "tsrroMieBaaa " "VvoolE421 "