snippets

String Concatenation & Interpolation

Hvordan man samler og indsætter strings i JavaScript og C#.

tstypescriptcsharpstrings

String Concatenation & Interpolation

To måder at bygge tekst på: concatenation samler strings med plus, mens interpolation indsætter værdier direkte i teksten.

Concatenation

ts · 7 lines

ts
1// section
2const firstName = "John";
3const lastName = "Doe";
4
5const fullName = firstName + " " + lastName;
6
7console.log(fullName);
Interpolation

ts · 7 lines

ts
1// section
2const firstName = "John";
3const age = 25;
4
5const message = `Hej ${firstName}, du er ${age} år gammel`;
6
7console.log(message);
C# Interpolation

csharp · 7 lines

csharp
1// section
2string firstName = "John";
3int age = 25;
4
5string message = $"Hej {firstName}, du er {age} år gammel";
6
7Console.WriteLine(message);

Hvornår bruges hvad?

Concatenation bruges når du blot skal samle få strings.

Eksempel:firstName + " " + lastName

Interpolation bruges når teksten indeholder flere variabler og skal være mere læsbar.

Eksempel:Hej ${name}, du har ${points} point