R字符串处理

R 字符串处理



1 #字符串连接:

paste() #paste(..., sep = " ", collapse = NULL)

> paste("a","b","c",sep=">")

[1] "a>b>c"

2 字符串分割:

strsplit() #strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE)

> strsplit("a>b>c",">")

[[1]]

[1] "a" "b" "c"

3 #计算字符串的字符数:

nchar()

> nchar("a>b>c")

[1] 5

4 #字符串截取:

substr(x, start, stop)

> paste("a","b","c",sep=">")->mm

> substr(mm, 1, 3)

[1] "a>b"

substring(text, first, last = 1000000L)

> substring(mm, 1, 3)

[1] "a>b"

> substring(mm,1,1:3)

[1] "a"   "a>"  "a>b"

解释

1代表从第一个字母开始

1:3代表取(1,1),(1,2),(1,3)

substr(x, start, stop) <- value

以下是进行字符串的替换

substring(text, first, last = 1000000) <- value

#字符串替换及大小写转换:

chartr(old, new, x)

> chartr("a","dd",mm)

[1] "d>b>c"

注:只能替换相同的字符数

toupper(x)

> toupper(mm)

[1] "A>B>C"

tolower(x)

> tolower(mm)

[1] "a>b>c"

casefold(x, upper = TRUE)

> casefold(mm, upper = TRUE)

[1] "A>B>C"

红色的起着重要作用.FALSE的话就还是和以前一样了

 

你可能感兴趣的:(字符串处理)