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
102
votes
4 answers

If Python strings are immutable, why does it keep the same id if I use += to append to it?

Strings in Python are immutable, which means the value cannot be changed. However, when appending to the string in the following example, it looks like the original string memory is modified since the id remains the same: >>> s = 'String' >>> for i…
Master_Roshy
  • 1,245
  • 3
  • 12
  • 19
102
votes
7 answers

JavaScript String concatenation behavior with null or undefined values

As you may know, in JavaScript '' + null = "null" and '' + undefined = "undefined" (in most browsers I can test: Firefox, Chrome and IE). I would like to know the origin of this oddity (what the heck was in the head on Brendan Eich?!) and if there…
Doc Davluz
  • 4,154
  • 5
  • 30
  • 32
99
votes
10 answers

SQL Server String Concatenation with Null

I am creating a computed column across fields of which some are potentially null. The problem is that if any of those fields is null, the entire computed column will be null. I understand from the Microsoft documentation that this is expected and…
Alex
  • 75,813
  • 86
  • 255
  • 348
98
votes
9 answers

String concatenation vs. string substitution in Python

In Python, the where and when of using string concatenation versus string substitution eludes me. As the string concatenation has seen large boosts in performance, is this (becoming more) a stylistic decision rather than a practical one? For a…
gotgenes
  • 38,661
  • 28
  • 100
  • 128
95
votes
7 answers

How slow is Python's string concatenation vs. str.join?

As a result of the comments in my answer on this thread, I wanted to know what the speed difference is between the += operator and ''.join() So what is the speed comparison between the two?
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
94
votes
1 answer

How can I concatenate str and int objects?

If I try to do the following: things = 5 print("You have " + things + " things.") I get the following error in Python 3.x: Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate str (not "int") to…
90
votes
9 answers

Best practices/performance: mixing StringBuilder.append with String.concat

I'm trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA") …
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
87
votes
6 answers

golang convert "type []string" to string

I see some people create a for loop and run through the slice as to create a string, is there an easier way to convert a []string to a string? Will sprintf do it?
user3888307
  • 2,825
  • 5
  • 22
  • 32
84
votes
4 answers

Concatenating strings doesn't work as expected

I know it is a common issue, but looking for references and other material I don't find a clear answer to this question. Consider the following code: #include // ... // in a method std::string a = "Hello "; std::string b =…
Andry
  • 16,172
  • 27
  • 138
  • 246
84
votes
5 answers

String concatenation vs. interpolation in Ruby

I am just starting to learn Ruby (first time programming), and have a basic syntactical question with regards to variables, and various ways of writing code. Chris Pine's "Learn to Program" taught me to write a basic program like…
Jeff H.
  • 923
  • 1
  • 7
  • 9
82
votes
6 answers

String concatenation without '+' operator

I was playing with python and I realized we don't need to use '+' operator to concatenate static strings. But it fails if I assign it to a variable. For example: string1 = 'Hello' 'World' #1 works fine string2 = 'Hello' + 'World' #2 also works…
ibrahim
  • 3,254
  • 7
  • 42
  • 56
79
votes
6 answers

Getting strings recognized as variable names in R

Related: Strings as variable references in R Possibly related: Concatenate expressions to subset a dataframe I've simplified the question per the comment request. Here goes with some example data. dat <-…
Hendy
  • 10,182
  • 15
  • 65
  • 71
75
votes
3 answers

How to concatenate strings in a Windows batch file?

I have a directory for which I want to list all the .doc files with a ;. I know the following batch command echos all the files: for /r %%i In (*.doc) DO echo %%i But now I want to put them all in a variable, add a ; in between and echo them all at…
Fortega
  • 19,463
  • 14
  • 75
  • 113
74
votes
23 answers

How do I concatenate two strings in Java?

I am trying to concatenate strings in Java. Why isn't this working? public class StackOverflowTest { public static void main(String args[]) { int theNumber = 42; System.out.println("Your number is " . theNumber . "!"); …
test
  • 17,706
  • 64
  • 171
  • 244
72
votes
6 answers

Create a comma-separated strings in C#

I have an object which holds many values, and some of them (not all values from the object) need to be put in a CSV string. My approach was this: string csvString = o.number + "," + o.id + "," + o.whatever .... Is there is a better, more elegant…
grady
  • 12,281
  • 28
  • 71
  • 110