c sharp

C# Concatenate Strings

Two or more-character strings may be concatenated in computer programming and data processing to save space or to address them as a single object. In C# programming language, string concatenation is a common task. When a string is concatenated with any other type in C#, the result is always a string. If a string concatenation operand is null then the empty string is replaced. String values are rarely known until run time, string concatenation is usually done at run time. The primary purpose of this article is to introduce you to C# String Concatenation.

What is String Concatenation in C# in Ubuntu 20.04?

The string.Concat method is used to combine multiple String types or the String expression of one or more object ‘instances’ values. A concatenated string is always returned by the string concatenation method. Bypassing alternative types and numbers of parameters to this method can be overloaded. Concatenating two or more strings with different syntax forms is feasible.

Syntax of the String Concatenation Method in C# in Ubuntu 20.04

The string concatenation method has the following syntax:

public static String.Concat (string str1, string str2);

We use the String.Concat method for concatenation of the specified string. The specified strings are declared as variables and passed in the string concatenation method as parameters. All of these parameters are of the System.String type property and the returned value is also of the System.String type. This method returns the result of a string that is created from specified strings concatenated together in the string concatenation method.

How to Concatenate the String in C# in Ubuntu 20.04?

We can add the string with another string with the help of common ways. Let’s get started with concatenating the strings in C#. Take a look at the following examples to learn the basics of concatenating strings with common C# methods. We have used Ubuntu 20.04 for all of the executions.

Example # 1: Using String.Concat(string, string) method in C# in Ubuntu 20.04:

The following program uses the String.Concat(string, string) method to illustrate the string concatenation. The method will concatenate two strings as we have passed two strings in the string concatenation method. Let’s see the program implementation below.

Firstly, we have included the system library “Using System” through which we can access the system method, function, or built-in properties. The class is defined with the public keyword and assigns a name to a class as “StringConcat”. In the class, we have called the main function and in the main function body, there are three variable declarations. The variables are defined as “str1”, “str2”, and “str3” and these variables have a System.String type. The first variable “str1” and the second variable “str2” are assigned a string value. The value of the third variable, “str3,” is not set to it.

The writeLine method is used for printing the string values before the concatenation. Then, we have called the variable “str3” in which we have the String.Concat method. The String.Concat method takes these different strings “str1” and “str2” into a single variable “str3” and concatenates these two strings. The “str3” is passed to the writeLine method that displays the string concatenation.

The way we have concatenated the two strings is shown as the output in the following image.

Example # 2: Using String.Concat(string, string, string) Method in C# in Ubuntu 20.04

Concatenating three separate strings into a single string is done with the passing of the three strings in the string.Concat method. The method returns the result of the string formed by concatenating three strings.

The above program has four variables declared as “str1”, “str2”, “str3”, and “str4” and these variables are of type System.String. We have assigned the string values to three variables “str1”, “str2”, and “str3”. Then, we have the variable “str4” which is just declared with the above variables. Now, this variable has a String. The Concat method takes the three different string variables at a time to create a single string. The concatenated string will be printed by the writeLine method.

Using the string concatenation method, the output is returned as a single string.

Example # 3: Using String.Concat(string[]) Method in C# in Ubuntu 20.04

The following C# program demonstrates the way to use the Concat(string[]) method. The values of a String array are concatenated by using this method. Remember that the value of the specified string item should not be null otherwise it will throw an ArgumentNullException. This method can also throw an OutOfMemoryException, if the given array runs out of memory.

We have the main function block of the C# class in which we have defined an array of the typed string. The array is defined with the name “MyStr” and assigned string values to the specified array. Then we have a foreach loop that will display the elements of the specified string array “Mystr”. The string concatenation is done by calling the Concat(string[]) method in the writeLine method and the method concatenates the elements of the string array into a single string.

Below, we have the output of concatenated element of the string array.

Example # 4: Using Plus Operator for String Concatenation in C# in Ubuntu 20.04

In C#, the + operator can also be used with a String value. It is referred to as the concatenation operator. By concatenating the strings together, we can create a new string.

Here, we have two string type variables as “string1” and “string2”. These variables are initialized with the string values of some words. Then, we have created another variable as “ConcatString” in which we have assigned the above-declared variables and used the “+” operator between them. The “+” operator will add these two strings and the resultant string will be a single string.

The single string output is shown on the console screen.

Example # 5: Using StringBuilder for String Concatenation in C# in Ubuntu 20.04

When compared to simple string concatenation, using a StringBuilder can improve efficiency. The StringBuilder class is defined under the System.Text namespace. Either import it or reference this namespace directly in the object initialization.

The above program is required to include the “System.Text” with the keyword “Using”. The class is defined as “StringBuilderMethod” in which we have invoked the main function. In the main function block, we have created the “StringBuilder” class object “sb” and the object is instantiated with the “new” modifier for the StringBuilder method. For loop is used to iterate over the value of variable “a”. Then, the StringBuilder object “sb” is using the Append method which will append the values of “a”. The values of “a” are separated by a space in the Append method. As append() returns a reference to the StringBuilder, it can be dynamically linked.

The outcome from the above program is displayed on the console screen.

Conclusion

In conclusion, we went over an effective and easy method with running examples in the C# string concatenation article. The introduction definition and the syntax have been clearly explained. We have used a few of the methods to concatenate the string but there are many more which are already used by different programming languages. We’ve attempted to demonstrate each of these key string concatenation methods in C# using various examples that will undoubtedly assist you.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.