How can two strings be concatenated?

string concatenation java
how to concatenate two strings in c++
string concatenation python
best way to concatenate strings in java
string concatenation in c++
string concatenation javascript
how to concatenate two strings in java using scanner
string concatenation c

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_new = "GAD,AB"

Which function can do this for me?

paste()

is the way to go. As the previous posters pointed out, paste can do two things:

concatenate values into one "string", e.g.

> paste("Hello", "world", sep=" ")
[1] "Hello world"

where the argument sep specifies the character(s) to be used between the arguments to concatenate, or collapse character vectors

> x <- c("Hello", "World")
> x
[1] "Hello" "World"
> paste(x, collapse="--")
[1] "Hello--World"

where the argument collapse specifies the character(s) to be used between the elements of the vector to be collapsed.

You can even combine both:

> paste(x, "and some more", sep="|-|", collapse="--")
[1] "Hello|-|and some more--World|-|and some more"

Hope this helps.

Methods to concatenate string in C/C++ with Examples , Concatenate Two Strings in Java. Using + Operator. The + operator is one of the easiest way to concatenate two Strings in Java that is used by vast majority of Java developers. Using String.concat() method. We can also use concat() method to concatenate one string to the end of another string. Using StringBuilder or Paste is used to convert the arguments to character strings and separates them by the string given by sep. In case of vector arguments, they are concatenated term-by-term to give a character vector result.

help.search() is a handy function, e.g.

> help.search("concatenate")

will lead you to paste().

String and Writing Programs, These are considered two separate types of objects. So, if you want to merge the two, you will need to convert the integer to a string. The following  Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

For the first non-paste() answer, we can look at stringr::str_c() (and then toString() below). 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. 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. If collapse is NULL each row is collapsed into a single string. If non-NULL that string is inserted at the end of each row, and the entire matrix collapsed to a single string.

Added 4/13/2016: It's not exactly the same as your desired output (extra space), but no one has mentioned it either. toString() is basically a version of paste() with collapse = ", " hard-coded, so you can do

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

Concatenate Two Strings in Java, Syntax: string new_string = string init + string add; This is the most easiest method for concatenation of two string. The + operator simply adds the two string and returns a concatenated string. I am trying to concatenate strings in Java. It isn't working? Recent in Java. How to manage two JRadioButtons in java so that only one of them can be selected at a time? 1 day ago

As others have pointed out, paste() is the way to go. But it can get annoying to have to type paste(str1, str2, str3, sep='') everytime you want the non-default separator.

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):

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. It's slightly more efficient than paste().

String Concatenation and Formatting, You can merge/concatenate/combine two Java String fields using the + operator, as shown in this example code: // define two strings String  In SQL Server, you can concatenate two or more strings by using the T-SQL CONCAT() function. You can also use SQL Server’s string concatenation operator (+) to do the same thing. Both are explained here. In SQL Server (and in any computer programming environment), string concatenation is the operation of joining character strings end-to-end.

> tmp = paste("GAD", "AB", sep = ",")
> tmp
[1] "GAD,AB"

I found this from Google by searching for R concatenate strings: http://stat.ethz.ch/R-manual/R-patched/library/base/html/paste.html

Java String concatenation: How to combine (merge) two Strings , In this C programming example, you will learn to concatenate two strings manually without using the strcat() function. String concatenation means to append one string to the end of another string. SQL allows us to concatenate strings but the syntax varies according to which database system you are using. Concatenation can be used to join strings from different sources including column values, literal strings, the output from user-defined functions or scalar sub-queries, etc.

C Program to Concatenate Two Strings, paste(). is the way to go. As the previous posters pointed out, paste can do two things: concatenate values into one "string", e.g. > paste("Hello"  Summary of concatenating strings in Python. In this tutorial, five ways of string concatenation in Python are explained with examples. You should choose + or += for the small number of string. For large numbers, either use the join() method or StringIO – the official recommended way for efficiency.

How can two strings be concatenated?, It is a basic function and rather an useful one, to concentrate strings in r use paste​() function, see this syntax for instance: paste(…, sep=""  The CONCATENATE function in Excel joins two or more text strings into one. For example, =CONCATENATE(B2,' ',C2) combines the text in cells B2 and C2, adding a space between the text.

How can two strings be concatenated?, Concatenation is the process of appending one string to the end of another string​. You concatenate strings by using the + operator. For string  s = strcat (s1,,sN) horizontally concatenates s1,,sN. Each input argument can be a character array, a cell array of character vectors, or a string array. If any input is a cell array, and none are string arrays, then the result is a cell array of character vectors. If all inputs are character arrays, then the result is a character array.

Comments
  • Most answers here break if the strings are vectors, as @RichardScriven's answer notes.
  • @smci what about the small answer i have posted? any suggestions to improve it?
  • Mixing up strings and vectors or vectors of different lengths is a bit too flexible in paste() for my liking. For instance, paste(c('a','b'),'blah', c(1,2,3)) results in "a blah 1" "b blah 2" "a blah 3". Basically, it creates a vector of strings the same length as the longest vector that's passed in, and loops the other vectors/strings to the same length. Plenty of room for accidental behaviour there.
  • True - but can you provide an alternative approach which addresses the question?
  • no - your answer is correct (as are most of the other answers that say the same thing). I was just noting that paste's behaviour is unusual in its flexibility.
  • @naught101 I wouldn't consider it unusual by R's standards. Vector recycling is a common property of R functions. Keep in mind that 'blah' is a vector of length 1. The recycling property makes it easy to do something like paste0("blah", 1:3) to get "blah1" "blah2" "blah3".
  • Yes, I should be complaining about R, not just paste :P . It's actually inconsistent across R - data.frame() doesn't let you do it if vectors aren't a multiple of each other. matrix() spits out warnings, but array() doesn't. Kind of annoying. Really, they should all spit out warnings unless some option is set...
  • Heh, this is the only answer that addresses the fact that tmp is a vector, and not just a bunch of values - paste doesn't do vectors. The other option is do.call(paste, as.list(tmp)).
  • So what the point posting a paste answer 4 years later when there are about a dozen of paste answers already?
  • At the time I found it helpful to summarize multiple answers for myself. The objective was not to collect votes but to help others filter through the many offered solutions. Often that's what I'm looking for.
  • for C programmers dealing with R, sprintf is familiar and useful for "concatenating two strings"
  • Much better imho. paste is not flexible enough if you want to append something to a string.
  • @Richard Scriven mayby I don't understand, but it seems straightforward, compare : paste0(as.matrix(iris[1:4]) , as.matrix(iris[1:4])) and as.matrix(iris[1:4]) %&% as.matrix(iris[1:4])
  • very very good! & is standard for concatenation in many languages, I actually think R should have had it by default. strongly recommend this way
  • Ok but stringi, stringr libraries are faster.