Concatenate A String

You will find many cases that you need to programmatically build a string by concatenating two or more strings together. You can work with two variables of string data type and join them together into a single string. Programmers call this process string concatenation. You will use the + operator in C# to concatenate the two strings together.

You can format a message to the user by using a combination of string variables and literals that are in quotation marks. For example, say you have a variable like strUserName and you have populated that variable with the User's Name. You want to format a message which welcomes the user using their name. To do this, you would use the statement "Welcome, " + strUsername.

There are several ways to concatenate two or more string sources. You can use either the arithmetic operator + operator or the += assignment operator. The + operator would be used to combine strings in the order that they appear in the expression. The + = assignment would be used to append a string to an existing string. Remember that as you append your strings, you will have to include the spacing inside the double quotes of your string to have proper spacing between words.

CONCATENATE A STRING

¿5 Untitled Notepad

CONCATENATE A STRING

¿5 Untitled Notepad

D Open your text editor.

^0 Type using System; to import the System namespace and press Enter.

0 Type the name of the class you want to create and press Enter.

—0 Between the curly braces, create the Main function.

0 Declare two string variables.

—Q Concatenate the strings together.

D Open your text editor.

^0 Type using System; to import the System namespace and press Enter.

0 Type the name of the class you want to create and press Enter.

^Q Type { }, placing the opening and closing curly braces on separate lines, to set off the body of the class.

—0 Between the curly braces, create the Main function.

0 Declare two string variables.

—Q Concatenate the strings together.

Was this article helpful?

0 0

Post a comment