A string is an empty space, a character, a word, or a group of words.
A string is an empty space, a character, a word, or a group of words that you want the compiler to consider “as is”, that is, not to pay too much attention to what the string is made of, unless you explicitly ask it to. This means that, in the strict sense, you can put in a string anything you want.
Primarily, the value of a string starts with a double quote and ends with a double-quote.
An example of a string is “Welcome to the World of C# Programming!”. You can include such a string in the Console.Write () method to display it on the console. Here is an example:
We’ll be covering the following topics in this tutorial:
Simple string
using System;
class Bookclub
{
static void Main()
{
console.writeline(“welcome to the world of c# programming!”) ;
}
}
OUTPUT:
welcome to the world of c# programming!
Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the string keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is:
string Msg;
After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. Here are examples of string variables declared and initialized:
string Empty = “”;
string Gender = “F”;
string FName = “Mahatama Gandhi”;
string Msg = “welcome to the world of c# programming! “;
After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using the Console.Write () or the Console.WriteLine () methods. Here is an example:
Use of string variable
using system;
class Bookclub
{
static void Main()
{
string Msg = “welcome to the world of C# programming! “;
console.writeLine(Msg);
}
}
OUTPUT:
Welcome to the World of C# Programming!
There are two types of string in C#:
1. Immutable strings
2. Mutable strings
The immutable strings are can’t be modify while mutable strings are modifiable.C# also supports a feature of regular expression that can be used for complex strings manipulations and pattern matching.
Handling of Strings
We can create immutable strings using string or String objects in a number of ways.
There are some techniques to handling the immutable strings:
Assigning String
string s1;
s1 = “welcome”;
or
string s1 =”welcome”;
Copying String
string s2 = s1;
or
string s2 = string.copy(s1);
Concatenating Strings
string s3 =s1 + s2;
or
string s3 = string.Concat(sl,s2);
Reading from Console
string sl = console.ReadLine(); .
Converting Number to String
int num = 100 ;
string s1= num.Tostring();
Inserting String
string s1 = “wel”
string s2 = s1.Insert(3,”come”);
II s2 = welcome
string s3 = s1.Insert(3,”don”);
/ / s3 = we1don;
Comparing Strings
int n = string.Compare(sl,s2);
This statement will perform case-sensitive comparison and returns integer values for different conditions. Such as:
• If sl is equal to s2 it will return zero.
• If sl is greater than s2 it will return positive integer (1).
• If sl is less than s2 it will return negative integer(-l).
Or you can use following statement:
bool a = s2.Equals(sl);
bool b = string.Equal(sl,s2);
Above statements will return a Boolean value true (if equal) or false (if not equal).
Or you can also use the “==” operator for comparing the strings. Like as:
if (s1 == s2)
console.write (” both are equal”);
In this statement, it will return a Boolean value true (if equal) or false (if not equal
Mutable String
Mutable strings are those strings, which can be modifying dynamically. This type of strings are created using StringBuilder class. For Example:
StringBuilder sl = new StringBuilder (“welcome”);
StringBuilder s2 = new StringBuilder ( );
The string sl is created with an initial size of seven characters and s2 is created as an empty string. They can grow dynamically as more character added to them. Mutual string are also referred as a dynamic strings. Mutable string can be modify dynamically.
The StringBuilder class supports many methods that are useful for manipulating dynamic strings. Some of the most common methods are listed below:
StringBuilder also provides some attributes to access some properties of strings, such as:
Here is an example program for mutable string. To use string builder class we have to use System. Text in the program.
Mutable string
using system.Text; // For using StringBuilder
using system;
class strMethod
{
public static void Main( )
{
StringBuilder s = new stringBui1der(“c”};
console.writeLine(” stored string is :”+ s);
console.writeLine(“Length of string is :”+s.Length);
s.Append(“Sharp “); II appending the string s
Console.writeLine(“After Append String is :”+ s);
console.writeLine(“Length of string is :”+s.Length);
//space will be count in “sharp” string.
s.Insert(7,”Language”); II inserting the string at last in s
console.writeLine(“After Insertion String is:”+ s);
console.writeLine(“Length of string is :”+s.Length);
int n = s.Length;
s [n] = “!”;
console.writeLine (” At Last String is:”+ s);
}
}
OUTPUT:
Stored string is : C
Length of string is 1
After Append string is : cSharp
Length of string is : 7
After Insertion string is : cSharp Language
Length of string is: 15
At Last String is : cSharp Language!