R语言字符串函数
  TnD0WQEygW8e 2023年11月08日 46 0

字符串长度:

nchar("hello world")

#字符串连接:
paste() #paste(..., sep = " ", collapse = NULL)

#字符串分割:
strsplit() #strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE)

aa<-unlist(strsplit(x, 'char'))

aa[1] # first string

#计算字符串的字符数:
nchar()

#字符串截取:
substr(x, start, stop)
substring(text, first, last = 1000000)
substr(x, start, stop) <- value
substring(text, first, last = 1000000) <- value

 substr("abcdef", 2, 4)
[1] "bcd"

> x <- "1234567890"
> substr(x, 3, 3)
[1] "3"
> 
> substr(x, 5, 7)
[1] "567"
> 
> substr(x, 4, 4) <- "A"
> x
[1] "123A567890"
> 
> substr(x, 2, 4) <- "TTF"
> x
[1] "1TTF567890"
> 
> substr(x, 9, 12) <- "ABCD"
> x
[1] "1TTF5678AB"
> 
> substring(x, 5)
[1] "5678AB"
> 
> substring(x, 5) <- "..."
> x
[1] "1TTF...8AB"


#字符串替换及大小写转换:
chartr(old, new, x)
tolower(x)
toupper(x)
casefold(x, upper = FALSE)

x=gsub(old_strin,,) # replace string 字符串替换

## 包含子字符串: chars中是否包含value

grepl(value,) # chars contains value? (TRUE, FALSE)

 

#字符串比较:

if(as.character(x)==as.character(y))


判断字符串是否包含某个子串
> chars <- "test"
> value <- "es"
> grepl(value, chars)



【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
TnD0WQEygW8e