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
69
votes
2 answers

Is python += string concatenation bad practice?

I am reading The Hitchhiker’s Guide to Python and there is a short code snippet foo = 'foo' bar = 'bar' foobar = foo + bar # This is good foo += 'ooo' # This is bad, instead you should do: foo = ''.join([foo, 'ooo']) The author pointed out that…
nos
  • 19,875
  • 27
  • 98
  • 134
66
votes
4 answers

How to collapse a list of characters into a single string in R

There is a list which I would like to output into an excel file as a single string. I start with a list of characters. url="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=21558518&retmode=xml" xml =…
userJT
  • 11,486
  • 20
  • 77
  • 88
65
votes
2 answers

Efficiently repeat a character/string n times in Scala

I would like to do the following more efficiently: def repeatChar(char:Char, n: Int) = List.fill(n)(char).mkString def repeatString(char:String, n: Int) = List.fill(n)(char).mkString repeatChar('a',3) // res0: String = aaa repeatString("abc",3)…
TimY
  • 5,256
  • 5
  • 44
  • 57
65
votes
8 answers

How to construct a std::string from a std::vector?

I'd like to build a std::string from a std::vector. I could use std::stringsteam, but imagine there is a shorter way: std::string string_from_vector(const std::vector &pieces) { std::stringstream ss; …
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
65
votes
3 answers

Ternary operator and string concatenation quirk?

Hi I just want to know why this code yields (at least for me) an incorrect result. Well, probably i'm in fault here $description = 'Paper: ' . ($paperType == 'bond') ? 'Bond' : 'Other'; I was guessing that if paperType equals 'Bond' then…
Cesar
  • 4,076
  • 8
  • 44
  • 68
63
votes
19 answers

Why to use StringBuffer in Java instead of the string concatenation operator

Someone told me it's more efficient to use StringBuffer to concatenate strings in Java than to use the + operator for Strings. What happens under the hood when you do that? What does StringBuffer do differently?
harry
62
votes
6 answers

PHP string concatenation

Is it possible to concatenate strings, as follows? And if not, what is the alternative of doing so? while ($personCount < 10) { $result += $personCount . "person "; } echo $result; It should appear like 1 person 2 person 3 person, etc. You…
Illep
  • 16,375
  • 46
  • 171
  • 302
60
votes
5 answers

Ampersand vs plus for concatenating strings in VB.NET

In VB.NET, is there any advantage to using & to concatenate strings instead of +? For example Dim x as String = "hello" + " there" vs. Dim x as String = "hello" & " there" Yes, I know for a lot of string concatenations I'd want to use…
dcp
  • 54,410
  • 22
  • 144
  • 164
60
votes
8 answers

Adding quotes to a string in VBScript

I have this code: a = "xyz" g = "abcd " & a After running it, the value of g is abcd xyz. However, I want quotes around the value of a in g. After running the code, g should be abcd "xyz" instead. How can I accomplish this?
sushant
  • 923
  • 5
  • 17
  • 33
56
votes
5 answers

Concatenation of Strings and characters

The following statements, String string = "string"; string = string +((char)65) + 5; System.out.println(string); Produce the output stringA5. The following however, String string = "string"; string += ((char)65) +…
Tiny
  • 27,221
  • 105
  • 339
  • 599
55
votes
6 answers

How do you concatenate strings in a Puppet .pp file?

Here is my naive approach: # puppet/init.pp $x = 'hello ' + 'goodbye' This does not work. How does one concatenate strings in Puppet?
rlandster
  • 7,294
  • 14
  • 58
  • 96
54
votes
1 answer

Concatenating variables in Bash

I am trying to add a variable to the middle of a variable, so for instance in PHP I would do this: $mystring = $arg1 . '12' . $arg2 . 'endoffile'; so the output might be 20121201endoffile, how can I achieve the same in a linux bash script?
Dan Hall
  • 1,474
  • 2
  • 18
  • 43
53
votes
3 answers

Concat strings in a shell script

How can I concat strings in shell? Is it just... var = 'my'; var .= 'string'; ?
Ben
  • 60,438
  • 111
  • 314
  • 488
52
votes
6 answers

How to concatenate a fixed string and a variable in Python

I want to include a file name, 'main.txt', in the subject. For that I am passing a file name from the command line. But I get an error in doing so: python sample.py main.txt # Running 'python' with an argument msg['Subject'] = "Auto Hella Restart…
Shivam Agrawal
  • 2,053
  • 4
  • 26
  • 42
47
votes
5 answers

Making a string concatenation operator in R

I was wondering how one might go about writing a string concatenation operator in R, something like || in SAS, + in Java/C# or & in Visual Basic. The easiest way would be to create a special operator using %, like `%+%` <- function(a, b) paste(a, b,…
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187