R String Functions


R string functions include substr(x), nchar(x), toupper(x), tolower(x), strsplit(x,y),paste(...), and regular expression functions sub(...), grep(...) etc.

>s <- "Perschon.com R Language Tutorial"
>substr(s,0,7)

[1] "Perschon"


Get string length:

>nchar(s)

[1] 31


To uppercase:

>x <- toupper(s)
>x

[1] "perschon.COM R LANGUAGE TUTORIAL"


To lowercase:

>x <- tolower(s)
>x

[1] "perschon.com r language tutorial"


Split the string at letter "o":

>x <- strsplit(s,"o")

[[1]]
[1] "EndMem" ".c" "m R Language Tut" "rial"


Concatenate two strings:

>x <- paste(x," -- String Functions",sep="")
>x

[1] "perschon.com r language tutorial -- String Functions"


Substring replacement:

>x <- sub("Tutorial","Examples",s)
>x

[1] "Perschon.com R Language Examples"


Use regular expression:

>x <- sub("n.+e","XXX",s)
>x

[1] "EXXX Tutorial"


Please see grep() function for more regular expression handling of string.