Questions tagged [string-concatenation]

String concatenation is the operation of joining two character strings end-to-end.

In particular in the theory of computation, the concatenation operation on strings is generalized to an operation on sets of strings as follows:

For two sets of strings S1 and S2, the concatenation S1S2 consists of all strings of the form vw where v is a string from S1 and w is a string from S2. In this definition, the string vw is the ordinary concatenation of strings v and w as defined in the introductory section. In this context, sets of strings are often referred to as formal languages.

Programming languages often provide operators (such as the + sign) to simplify concatenation of strings. For example, "s1" + "s2" would result in "s1s2".

2012 questions
20
votes
2 answers

How can I print all the elements of a vector in a single string in R?

Sometimes I want to print all of the elements in a vector in a single string, but it still prints the elements separately: notes <- c("do","re","mi") print(paste("The first three notes are: ", notes,sep="\t")) Which gives: [1] "The first three…
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
20
votes
10 answers

What better way to concatenate string in python?

Understand "better" as a quicker, elegant and readable. I have two strings (a and b) that could be null or not. And I want concatenate them separated by a hyphen only if both are not null: a - b a (if b is null) b (where a is null)
Shankar Cabus
  • 9,302
  • 7
  • 33
  • 43
20
votes
2 answers

Is string concatenaion really that slow?

I'm currently looking into String concat options and the penalty they have on the overall performance. And my test-case creates results that blow my mind, I'm not sure if I'm overlooking something. Here is the deal: Doing "something"+"somethingElse"…
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
20
votes
7 answers

SQL: Concatenate column values in a single row into a string separated by comma

Let's say I have a table like this in SQL Server: Id City Province Country 1 Vancouver British Columbia Canada 2 New York null null 3 null Adama null 4 …
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275
19
votes
4 answers

Dart : Printing integer along with string

Consider the below code. void main() { int num = 5; print('The number is ' + num); } When I try to print the value for the variable num, I get the exception : The argument type 'int' can't be assigned to the parameter type 'String'. How do I…
Arun George
  • 1,167
  • 3
  • 15
  • 29
19
votes
3 answers

What is the difference of + operator and concat() method in JavaScript

The plus ( + ) operator and String.concat() method gives the same result. plus ( + ) operator; str1 + str2; String concat() method; str1.concat(str2); Addionally, it is written in w3schools ; But with JavaScript, methods and properties are also…
18
votes
2 answers

Java 8: Class.getName() slows down String concatenation chain

Recently I've run into an issue regarding String concatenation. This benchmark summarizes it: @OutputTimeUnit(TimeUnit.NANOSECONDS) public class BrokenConcatenationBenchmark { @Benchmark public String slow(Data data) { final Class
18
votes
3 answers

Python Pandas concatenate a Series of strings into one string

In python pandas, there is a Series/dataframe column of str values to combine into one long string: df = pd.DataFrame({'text' : pd.Series(['Hello', 'world', '!'], index=['a', 'b', 'c'])}) Goal: 'Hello world !' Thus far methods such as…
cycle_about
  • 325
  • 1
  • 3
  • 6
18
votes
2 answers

How do I concatenate strings in Entity Framework Query?

How do I concatenate strings in Entity Framework 4 I have a data from a column and I want to save as a string a comma separated string like "value1, value2, value3" Is there a method or an operator do do this in EF4? Example: lets say that I have…
17
votes
2 answers

Android StringBuilder vs String Concatenation

I was reading this documentation page, http://developer.android.com/reference/android/util/Log.html. The section here caught my eye: Tip: Don't forget that when you make a call like Log.v(TAG, "index=" + i); that when you're building the string to…
Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99
17
votes
10 answers

How should I concatenate strings?

Are there differences between these examples? Which should I use in which case? var str1 = "abc" + dynamicString + dynamicString2; var str2 = String.Format("abc{0}{1}", dynamicString, dynamicString2); var str3 = new StringBuilder("abc"). …
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
17
votes
9 answers

Removing last comma from a foreach loop

I'm using a foreach loop to echo out some values from my database and separating each of them by commas. I don't know how to remove the last comma it adds on the last value. My code is pretty simple, but I just can't seem to find the correct way of…
Victor York
  • 1,621
  • 4
  • 20
  • 55
16
votes
1 answer

Can MySQL concatenate strings with ||

I'm using sqlite3 for the moment, and hence concatenation strings using the || operator. At some later date I'd like to shift to MySQL, and hence it would be nice if no changes to the code had to be made. I'd normally use concat() to concatenate in…
Rahul Sekhar
  • 2,761
  • 5
  • 23
  • 27
16
votes
8 answers

Java concatenate to build string or format

I'm writing a MUD (text based game) at the moment using java. One of the major aspects of a MUD is formatting strings and sending it back to the user. How would this best be accomplished? Say I wanted to send the following string: You say to…
two13
  • 345
  • 1
  • 3
  • 11
16
votes
1 answer

Why is it so slow when assigning a concatenated string to a variable in python?

If it is only concatenation of strings as follows, it finish immediately. test_str = "abcdefghijklmn123456789" str1 = "" str2 = "" start = time.time() for i in range(1, 100001): str1 = str1 + test_str str2 = str2 + test_str if i %…
uma66
  • 175
  • 7