How to Concatenate String in C#

Problem:

Create a C# program that will allow the user to enter two strings and then the program will combine those strings to form a new one.

Solution:

We will use the plus sign (+) to combine two strings to form a new string. This tutorial will teach us to concatenate two strings in two format; the gui and the console format.

GUI means with the use of graphical interface such as button, form, etc.

Console is like command prompt with command line interfaces.

We’ll start with Windows Form Application:

  1. Open your Microsoft Visual Studio 2010.
  2. File, New Project.
  3. Select C# then Windows Form Application and click OK.
  4. We will add 2 labels, 2 textbox and 1 button.
    c#combinestring1
  5. The user will enter his/her firstname and lastname, then they will click the Show Fullname button to display the fullname. This shows how to combine two strings to become one.
  6. Double click the button and paste the code.
if (textBox1.Text == "")
{
MessageBox.Show("please enter a string");
textBox1.Focus();
}
else if (textBox2.Text=="")
{
MessageBox.Show("please enter a string");
textBox2.Focus();
}
else
{
MessageBox.Show(textBox1.Text + " " + textBox2.Text);
}

Code Explanation:

The if else statement is used to validate if the user did not leave the field blank or empty. After that the program will combine the firstname and lastname of the user to form his/her fullname.

  1. Save and Run the Project.

Console Application:

  1. Open MS Visual Studio 2010
  2. File then New Project.
  3. Select C# then Console Application and click OK.
  4. This will be the code that you will see:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}
  1. paste the code below right after the 3rd open { symbol
string lname, fname;
Console.WriteLine("Please enter firstname:");
fname = Console.ReadLine();
Console.WriteLine("Please enter lasname:");
lname = Console.ReadLine();
Console.WriteLine("Fullname: " + fname + " " + lname);
Console.ReadLine();
  1. to make sure you are doing thing right, here’s the full code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string lname, fname;
Console.WriteLine("Please enter firstname:");
fname = Console.ReadLine();
Console.WriteLine("Please enter lasname:");
lname = Console.ReadLine();
Console.WriteLine("Fullname: " + fname + " " + lname);
Console.ReadLine();
}
}
}

Code Explanation:

  1. We will first declare two variables (lname and fname)
  2. The program will ask the user to enter the firstname, after that the program will read the input of the user.
  3. Same with the lastname.
  4. The program will then combine the firstname and lastname to form the fullname.
  1. Test and save the project.

c#combinestring2
Download

, , , , ,

Post navigation