Questions tagged [string-concatenation]
String concatenation is the operation of joining two character strings end-to-end.
2,043
questions
3535
votes
31
answers
5.0m
views
How to concatenate string variables in Bash
In PHP, strings are concatenated together as follows:
$foo = "Hello";
$foo .= " World";
Here, $foo becomes "Hello World".
How is this accomplished in Bash?
2418
votes
48
answers
3.3m
views
How to concatenate text from multiple rows into a single text string in SQL Server
Consider a database table holding names, with three rows:
Peter
Paul
Mary
Is there an easy way to turn this into a single string of Peter, Paul, Mary?
1175
votes
30
answers
845k
views
Shortcuts in Objective-C to concatenate NSStrings
Are there any shortcuts to (stringByAppendingString:) string concatenation in Objective-C, or shortcuts for working with NSString in general?
For example, I'd like to make:
NSString *myString = @"...
983
votes
20
answers
699k
views
How to efficiently concatenate strings in go
In Go, a string is a primitive type, which means it is read-only, and every manipulation of it will create a new string.
So if I want to concatenate strings many times without knowing the length of ...
972
votes
22
answers
1.9m
views
Combine two columns of text in pandas dataframe
I have a dataframe that looks like
Year quarter
2000 q2
2001 q3
How do I add a new column by combining these columns to get the following dataframe?
Year quarter period
2000 q2 ...
954
votes
22
answers
2.3m
views
How do I concatenate strings and variables in PowerShell?
Suppose I have the following snippet:
$assoc = New-Object PSObject -Property @{
Id = 42
Name = "Slim Shady"
Owner = "Eminem"
}
Write-Host $assoc.Id + " - " ...
587
votes
9
answers
475k
views
How do I concatenate strings?
How do I concatenate the following combinations of types:
str and str
String and str
String and String
547
votes
11
answers
528k
views
How to concatenate strings in twig
Anyone knows how to concatenate strings in twig? I want to do something like:
{{ concat('http://', app.request.host) }}
494
votes
23
answers
869k
views
How to use GROUP BY to concatenate strings in SQL Server?
How do I get:
id Name Value
1 A 4
1 B 8
2 C 9
to
id Column
1 A:4, B:8
2 C:9
475
votes
12
answers
925k
views
How can two strings be concatenated?
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 ...
424
votes
16
answers
191k
views
How to keep the spaces at the end and/or at the beginning of a String?
I have to concatenate these two strings from my resource/value files:
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string>
<string name="...
406
votes
17
answers
398k
views
Using LINQ to concatenate strings
What is the most efficient way to write the old-school:
StringBuilder sb = new StringBuilder();
if (strings.Count > 0)
{
foreach (string s in strings)
{
sb.Append(s + ", ");
}
...
404
votes
16
answers
616k
views
String concatenation in Ruby
I am looking for a more elegant way of concatenating strings in Ruby.
I have the following line:
source = "#{ROOT_DIR}/" << project << "/App.config"
Is there a nicer way of doing this?
...
293
votes
7
answers
494k
views
How to set the id attribute of a HTML element dynamically with angularjs (1.x)?
Provided an HTML element of type div, how to set the value of its id attribute, which is the concatenation of a scope variable and a string ?
256
votes
11
answers
298k
views
C++ equivalent of StringBuffer/StringBuilder?
Is there a C++ Standard Template Library class that provides efficient string concatenation functionality, similar to C#'s StringBuilder or Java's StringBuffer?
241
votes
3
answers
277k
views
How to concatenate strings with padding in sqlite
I have three columns in an sqlite table:
Column1 Column2 Column3
A 1 1
A 1 2
A 12 2
C 13 2
B ...
214
votes
13
answers
174k
views
How to create a SQL Server function to "join" multiple rows from a subquery into a single delimited field? [duplicate]
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 ...
213
votes
21
answers
356k
views
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 *...
212
votes
16
answers
113k
views
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....
209
votes
15
answers
154k
views
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), "" + ...
200
votes
6
answers
429k
views
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).
188
votes
5
answers
163k
views
Concatenating null strings in Java [duplicate]
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"
158
votes
9
answers
521k
views
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 ...
152
votes
7
answers
315k
views
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 = ...
148
votes
13
answers
328k
views
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 ...
139
votes
8
answers
224k
views
Optimal way to concatenate/aggregate strings
I'm finding a way to aggregate strings from different rows into a single row. I'm looking to do this in many different places, so having a function to facilitate this would be nice. I've tried ...
138
votes
8
answers
206k
views
Any reason not to use '+' to concatenate two strings? [duplicate]
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 ...
129
votes
10
answers
105k
views
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, ...
115
votes
3
answers
14k
views
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 ...
109
votes
6
answers
204k
views
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 ...
107
votes
4
answers
14k
views
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%...
107
votes
3
answers
255k
views
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:
...
104
votes
7
answers
105k
views
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 ...
102
votes
4
answers
7k
views
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 ...
100
votes
10
answers
186k
views
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 ...
98
votes
9
answers
85k
views
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 ...
96
votes
7
answers
96k
views
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?
95
votes
1
answer
114k
views
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 "<...
90
votes
9
answers
91k
views
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 ...
90
votes
6
answers
194k
views
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?
87
votes
6
answers
101k
views
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 ...
85
votes
5
answers
37k
views
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 ...
84
votes
4
answers
223k
views
Concatenating strings doesn't work as expected [closed]
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 <string>
// ...
// in a method
...
81
votes
6
answers
173k
views
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.
...
76
votes
3
answers
218k
views
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 ...
75
votes
23
answers
478k
views
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....
74
votes
2
answers
114k
views
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 = '...
73
votes
6
answers
151k
views
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 + &...
67
votes
4
answers
91k
views
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=...
65
votes
8
answers
107k
views
How to construct a std::string from a std::vector<string>?
I'd like to build a std::string from a std::vector<std::string>.
I could use std::stringsteam, but imagine there is a shorter way:
std::string string_from_vector(const std::vector<std::...