String is important data type in every programming language. They are used to display text and involved in various language processing applications. So, this article quickly lists out several examples to create and manipulate a string from data in different programming languages.

In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literalcontaining one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing or, in formal terms, a form of quasi-quotation (or logic substitution interpretation). String interpolation allows easier and more intuitive string formatting and content-specification compared with string concatenation.

String in C

Store concatenated string in a buffer

String in C++

In C++, the same syntax of C can be applied. But there are a more C++ way by using string class.

You can use string format for a string interpolation:

String in C#

In C#, you can use the following syntax with $:

{<interpolatedExpression>[,<alignment>][:<formatString>]}

Note that: You cannot have any white space between the $ and the " that starts a string literal.

And another example of string with field data inserted:

String in Java

Java creates String object by concatenation.

We can also use string format() to create a string object:

 

String in Python

Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix f.

%-formatting

We can use format() function on a string object and braces {}, the string object in format() function is substituted in place of braces {}.

String in Dart

You can access the value of an expression inside a string by using ${expression}.

If the expression is an identifier, the {} can be skipped.

If the variable inside the {} isn’t a string, the variable’s toString() method is called:

String in Kotlin

String in SWIFT

We can insert variables in a String using \(var).

 

Follow me

Tung Nguyen

PhD/Researcher/Programmer at Up Education - YooBee Colleges
I received the B.Eng. Degree in telecommunication from Shanghai University, MSc of the same major from Paris-Sud University and a PhD degree in Computer Science from Auckland University of Technology. My research interests include machine learning, game theory, computational trust, multi-agent systems and software engineering.
Follow me

Latest posts by Tung Nguyen (see all)

Login with your social ID or Credentials