如何将两个字符串串联在一起?

本文翻译自:How can two strings be concatenated?

How can I concatenate (merge, combine) two values? 如何连接(合并,合并)两个值? For example I have: 例如,我有:

tmp = cbind("GAD", "AB")
tmp
#      [,1]  [,2]
# [1,] "GAD" "AB"

My goal is to concatenate the two values in "tmp" to one string: 我的目标是将“ tmp”中的两个值连接为一个字符串:

tmp_new = "GAD,AB"

Which function can do this for me? 哪个功能可以为我执行此操作?


#1楼

参考:https://stackoom.com/question/UDOf/如何将两个字符串串联在一起


#2楼

As others have pointed out, paste() is the way to go. 正如其他人指出的那样, paste()是必经之路。 But it can get annoying to have to type paste(str1, str2, str3, sep='') everytime you want the non-default separator. 但是每次想要非默认分隔符时都必须键入paste(str1, str2, str3, sep='')会很烦人。

You can very easily create wrapper functions that make life much simpler. 您可以非常轻松地创建包装函数,使生活变得更加简单。 For instance, if you find yourself concatenating strings with no separator really often, you can do: 例如,如果您发现自己实际上经常串联不带分隔符的字符串,则可以执行以下操作:

p <- function(..., sep='') {
    paste(..., sep=sep, collapse=sep)
}

or if you often want to join strings from a vector (like implode() from PHP): 或者,如果您经常想从向量中连接字符串(例如,PHP中的implode() ):

implode <- function(..., sep='') {
     paste(..., collapse=sep)
}

Allows you do do this: 允许您执行以下操作:

p('a', 'b', 'c')
#[1] "abc"
vec <- c('a', 'b', 'c')
implode(vec)
#[1] "abc"
implode(vec, sep=', ')
#[1] "a, b, c"

Also, there is the built-in paste0 , which does the same thing as my implode , but without allowing custom separators. 另外,还有内置的paste0 ,它与我的implode相同,但是不允许自定义分隔符。 It's slightly more efficient than paste() . 它比paste()略有效率。


#3楼

For the first non- paste() answer, we can look at stringr::str_c() (and then toString() below). 对于第一个non- paste()答案,我们可以看一下stringr::str_c() (然后看下面的toString() )。 It hasn't been around as long as this question, so I think it's useful to mention that it also exists. 这个问题还没有出现很久,所以我认为提到它也存在很有用。

Very simple to use, as you can see. 如您所见,它非常易于使用。

tmp <- cbind("GAD", "AB")
library(stringr)
str_c(tmp, collapse = ",")
# [1] "GAD,AB"

From its documentation file description, it fits this problem nicely. 从其文档文件描述中可以很好地解决此问题。

To understand how str_c works, you need to imagine that you are building up a matrix of strings. 要了解str_c的工作原理,您需要想象自己正在建立一个字符串矩阵。 Each input argument forms a column, and is expanded to the length of the longest argument, using the usual recyling rules. 每个输入参数形成一列,并使用通常的重新编制规则扩展为最长参数的长度。 The sep string is inserted between each column. sep字符串插入在每列之间。 If collapse is NULL each row is collapsed into a single string. 如果崩溃为NULL,则将每一行折叠为单个字符串。 If non-NULL that string is inserted at the end of each row, and the entire matrix collapsed to a single string. 如果为非NULL,则在每行末尾插入该字符串,并且整个矩阵折叠为单个字符串。

Added 4/13/2016 : It's not exactly the same as your desired output (extra space), but no one has mentioned it either. 添加了4/13/2016 :它与所需的输出(多余的空间)不完全相同,但是也没有人提及它。 toString() is basically a version of paste() with collapse = ", " hard-coded, so you can do toString()基本上是paste()的版本,其collapse = ", "硬编码,因此您可以执行

toString(tmp)
# [1] "GAD, AB"

#4楼

另外,如果您的目标是直接输出到文件或标准输出,则可以使用cat

cat(s1, s2, sep=", ")

#5楼

You can create you own operator : 您可以创建自己的运算符:

'%&%' <- function(x, y)paste0(x,y)
"new" %&% "operator"
[1] newoperator`

You can also redefine 'and' ( & ) operator : 您还可以重新定义'and'( & )运算符:

'&' <- function(x, y)paste0(x,y)
"dirty" & "trick"
"dirtytrick"

messing with baseline syntax is ugly, but so is using paste()/paste0() if you work only with your own code you can (almost always) replace logical & and operator with * and do multiplication of logical values instead of using logical 'and &' 用基线语法弄乱是很丑陋的,但是如果仅使用自己的代码,则使用paste()/paste0()也可以(几乎总是)用*替换逻辑& and运算符,并乘以逻辑值而不是使用逻辑'和&'


#6楼

Another way: 其他方式:

sprintf("%s you can add other static strings here %s",string1,string2)

It sometimes useful than paste() function. 它有时比paste()函数有用。 %s denotes the place where the subjective strings will be included. %s表示将包含主观字符串的位置。

Note that this will come in handy as you try to build a path: 请注意,这在您尝试构建路径时会派上用场:

sprintf("/%s", paste("this", "is", "a", "path", sep="/"))

output 输出

/this/is/a/path
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值