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
213
votes
13 answers

How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field?

To illustrate, assume that I have two tables as follows: VehicleID Name 1 Chuck 2 Larry LocationID VehicleID City 1 1 New York 2 1 Seattle 3 1 Vancouver 4 2 Los…
Templar
  • 5,067
  • 7
  • 34
  • 39
211
votes
21 answers

How do I concatenate strings in Swift?

How to concatenate string in Swift? In Objective-C we do like NSString *string = @"Swift"; NSString *resultStr = [string stringByAppendingString:@" is a new Programming Language"]; or NSString *resultStr=[NSString stringWithFormat:@"%@ is a new…
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
206
votes
14 answers

Android TextView : "Do not concatenate text displayed with setText"

I am setting text using setText() by following way. prodNameView.setText("" + name); prodOriginalPriceView.setText("" + String.format(getString(R.string.string_product_rate_with_ruppe_sign), "" + new BigDecimal(price).setScale(2,…
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
203
votes
16 answers

Can I use require("path").join to safely concatenate urls?

Is this safe to use require("path").join to concatenate URLs, for example: require("path").join("http://example.com", "ok"); //returns 'http://example.com/ok' require("path").join("http://example.com/", "ok"); //returns…
Renato Gama
  • 16,431
  • 12
  • 58
  • 92
200
votes
6 answers

What is the string concatenation operator in Oracle?

What is the string concatenation operator in Oracle SQL? Are there any "interesting" features I should be careful of? (This seems obvious, but I couldn't find a previous question asking it).
AJ.
  • 13,461
  • 19
  • 51
  • 63
188
votes
5 answers

Concatenating null strings in Java

Why does the following work? I would expect a NullPointerException to be thrown. String s = null; s = s + "hello"; System.out.println(s); // prints "nullhello"
yavoh
  • 2,645
  • 5
  • 24
  • 21
157
votes
9 answers

Concatenating string and integer in Python

In Python say you have s = "string" i = 0 print s + i will give you error, so you write print s + str(i) to not get error. I think this is quite a clumsy way to handle int and string concatenation. Even Java does not need explicit casting to…
specialscope
  • 4,188
  • 3
  • 24
  • 22
150
votes
7 answers

How do you append to an already existing string?

I want append to a string so that every time I loop over it, it will add "test" to the string. Like in PHP you would do: $teststr = "test1\n" $teststr .= "test2\n" echo = "$teststr" Returns: test1 test2 But I need to do this in a shell script
Mint
  • 14,388
  • 30
  • 76
  • 108
145
votes
13 answers

const char* concatenation

I need to concatenate two const chars like these: const char *one = "Hello "; const char *two = "World"; How might I go about doing that? I am passed these char*s from a third-party library with a C interface so I can't simply use std::string…
Anthoni Caldwell
  • 1,485
  • 2
  • 10
  • 4
138
votes
8 answers

Any reason not to use '+' to concatenate two strings?

A common antipattern in Python is to concatenate a sequence of strings using + in a loop. This is bad because the Python interpreter has to create a new string object for each iteration, and it ends up taking quadratic time. (Recent versions of…
Taymon
  • 24,950
  • 9
  • 62
  • 84
127
votes
10 answers

Why is string concatenation faster than array join?

Today, I read this thread about the speed of string concatenation. Surprisingly, string concatenation was the winner: http://jsben.ch/#/OJ3vo The result was opposite of what I thought. Besides, there are many articles about this which explain…
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
115
votes
3 answers

How is String concatenation implemented in Java 9?

As written in JEP 280: Indify String Concatenation: Change the static String-concatenation bytecode sequence generated by javac to use invokedynamic calls to JDK library functions. This will enable future optimizations of String concatenation…
Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29
107
votes
5 answers

SQL NVARCHAR and VARCHAR Limits

All, I have a large (unavoidable) dynamic SQL query. Due to the number of fields in the selection criteria the string containing the dynamic SQL is growing over 4000 chars. Now, I understand that there is a 4000 max set for NVARCHAR(MAX), but…
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
107
votes
3 answers

String concatenation with Groovy

What is the best (idiomatic) way to concatenate Strings in Groovy? Option 1: calculateAccountNumber(bank, branch, checkDigit, account) { bank + branch + checkDigit + account } Option 2: calculateAccountNumber(bank, branch, checkDigit, account)…
Arturo Herrero
  • 12,772
  • 11
  • 42
  • 73
105
votes
4 answers

Is the time-complexity of iterative string append actually O(n^2), or O(n)?

I am working on a problem out of CTCI. The third problem of chapter 1 has you take a string such as 'Mr John Smith ' and asks you to replace the intermediary spaces with %20: 'Mr%20John%20Smith' The author offers this solution in Python, calling…