617
In this example we will create a program that joins two strings together, or concatenate them
We do this by using the String.Concat Method
Example
In this example we ask the user to input two strings and then we concatenate them and display the concatenated string
using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string myString1 = ""; string myString2 = ""; string myString3 = ""; // Get first string from user Console.Write("Enter string1: "); myString1 = Console.ReadLine(); // Ger second string from user Console.Write("Enter string2: "); myString2 = Console.ReadLine(); // JOin the 2 strings myString3 = String.Concat(myString1, myString2); // Display the concatenated string Console.WriteLine("The concatenated string is: {0}", myString3); Console.WriteLine("Press any key to exit."); Console.ReadLine(); } } }
Here are a couple of test runs
Enter string1: Welcome to Enter string2: maxcsharp The concatenated string is: Welcome tomaxcsharp Press any key to exit. Enter string1: Welcome to Enter string2: maxcsharp The concatenated string is: Welcome to maxcsharp Press any key to exit.
In the second example we added a space before the maxcsharp
Notes
You can also use your language's string concatenation operator, such as + in C#