If you remember the lesson about char variable type, you know that a char can only store a single character. And you also remember that for storing more than a single character, I said you will be using the string variable type.
A string is basically a series of one or more characters. It’s default value is null. Unlike characters, which enclose their value between single quotation marks, strings enclose them in double quotation marks.
Lets see an example about declaring and initializing a string variable type:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
// Declare some variables
string firstName = "John";
string lastName = "Smith";
string fullName = firstName + " " + lastName;
// Print the results at the console
Console.WriteLine("Hello, " + firstName + "!");
Console.WriteLine("Your full name is " + fullName + ".");
Console.ReadLine();
}
}
}
|
The output of the above code will be this:
Various text-processing operations can be performed using strings: concatenation (joining one string with another), splitting by a given separator, searching, replacement of characters and others. I will be explaining all them in a later part of our lessons.
The concepts explained in this lesson are also shown visually as part of the following video: