What is a string ? Explain types of string used in C sharp with proper example .
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:
using System;
class Bookclub
{
static void Main()
{
console.writeline("welcome to the world of c# programming!") ;
}
}
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.
We can create immutable strings using string or String objects in a number of ways.
There are some techniques to handling the immutable strings:
string s1;
s1 = "welcome";
or
string s1 =”welcome”;
string s2 = s1;
or
string s2 = string.copy(s1);
string s3 =s1 + s2;
or
string s3 = string.Concat(sl,s2);
string sl = console.ReadLine(); .
int num = 100 ;
string s1= num.Tostring();
string s1 = "wel"
string s2 = s1.Insert(3,"come");
II s2 = welcome
string s3 = s1.Insert(3,"don");
/ / s3 = we1don;
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).
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 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 modified dynamically.
Here is an example program for mutable string. To use string builder class we have to use System. Text in the program.
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!