When switching between languages I often find my self Googling on how to do string concatenation and especially interpolation which no two languages seems to agree on.
For future reference, Here's how you do it.
C
In C, strings are really just plain char
arrays. Therefore, you can't directly concatenate them with other strings.
You can use the strcat
function, which appends the string pointed to by src
to the end of the string pointed to by dest
:
char *strcat(char *dest, const char *src);
C++
C++ has std::string
which is used instead of char
arrays.
string a = "Hello ";
string b = "World";
string c = a + b;
C#
C# lets you use the +
operator to concatenate strings.
string name = "Chris";
Console.WriteLine("Hello " + name);
It also allows for interpolation.
string firstName = "Sally";
string lastName = "Morgan";
Console.WriteLine($"Hello {firstName} {lastName}");
Go
You can use the +
operator to concatenate strings in Go.
firtName := "James"
lastName := "Pierce"
fmt.Println(firstName + " " + lastName)
There's also fmt.Sprintf
which lets you format strings.
firtName := "Denise"
lastName := "Richards"
fullName = fmt.Sprintf("%s %s", firstName, lastName)
Strings are read-only in Go so a new object is generated every time you concatenate two strings. If this becomes a problem you can use bytes.buffer
.
var buffer bytes.Buffer
buffer.WriteString("Hello")
buffer.WriteString(" ")
buffer.WriteString("World")
fmt.Println(buffer.String())
Java
Java also uses the +
operator to concatenate strings.
String name = "Molly";
System.out.println("Hello " + name);
Javascript
Javascript uses the +
operator to concatenate strings.
let firstName = 'Isabela';
let lastName = 'Wang';
console.log(firstName + ' ' + lastName);
You can also interpolate strings with backticks.
let firstName = 'Megan';
let lastName = 'Fox';
console.log(`Hello ${firstName} ${lastName}`);
PHP
PHP uses .
to concatenate strings.
$firstName = 'Lisa';
$lastName = 'Swan';
echo $firstname . ' ' . $lastName;
Only variables can be interpolated.
$name = 'Michelle';
echo "Hello {$name}";
Ruby
Ruby uses the +
operator to concatenate strings.
fist_name = 'Tyler'
last_name = 'Reed'
puts fist_name + ' ' + last_name
And it uses #{}
for interpolation
name = 'Sarah'
puts "Hello #{name}"
Python
Python uses the +
operator to concatenate strings.
firstName = "Emma"
lastName = "Stone"
print(firstName + " " + lastName)
And from version 3.6 you can interpolate strings.
name = "Sally"
print(f"Hello {name}")