我有一个字符串:
words<-"Monday, Tuesday, Wednesday, Thursday,Friday"
我只需要为每个单词添加引号:
"Monday", "Tuesday", "Wednesday", "Thursday","Friday"
获得五个字符串的长度。
我知道有很多关于这个主题的帖子,但我在R中找不到任何关于它的内容。
非常感谢。
答案 0 :(得分:8)
我们可以按,
分割单词以获得list
输出。我们遍历sapply
,dQuote
元素,然后将paste
与toString
一起循环,paste(..., collapse=', ')
是sapply(strsplit(words, '[, ]+'), function(x) toString(dQuote(x)))
#[1] "“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”"
的包装。
public static <T, R> Predicate<T> isNull(Function<T, R> mapper) {
return new Predicate<T> {
@Override
boolean test(T o) {
return ObjectPredicate.IS_NULL.callback().test(mapper.apply(o));
}
}
}
答案 1 :(得分:7)
使用gsub
words<-"Monday, Tuesday, Wednesday, Thursday,Friday"
cat(gsub("(\\w+)", '"\\1"', words))
# "Monday", "Tuesday", "Wednesday", "Thursday","Friday"
... KISS
cat(gsub("\\b", '"', words, perl=T))
#"Monday", "Tuesday", "Wednesday", "Thursday","Friday"
\\b
称为单词边界,它在单词字符( AZ,az,_,0-9 )和非单词字符(不是A-Za-z0-)之间匹配9_)或反之亦然..
答案 2 :(得分:3)
strsplit用逗号分割字符串,sub删除空格。
paste(dQuote(sub(" ","",unlist(strsplit(words,split = ",")))),collapse = ", ")
[1] "“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”"