string :- string is a collection of characters or we can say that it is a combination
of characters and always enclosed within double quotes " " in C#. strings are widely
used in any programming languages to perform opeartions like adding words together, finding any specific word pattern
and checking the length of words, trim the words and many more in real world programming.
string is an Object of String Class.
Example of string
//myName is the name of variable and "Rajveer Singh" is the value of variable
//It is the combination of R,a,j,v,e,e,r and S,i,n,g,h
string myName = "Rajveer Singh";
Graphical Representation for string Declaration and Initialisation
In details you can see in below program with output. Here we are displaying string value on console.
internal class Program
{
static void Main(string[] args)
{
string myName = "Rajveer Singh";
Console.WriteLine("Your Name is :- " + myName);
Console.ReadKey();
}
}
Output of Program :-
Your Name is :- Rajveer Singh
Concat :- Joining two strings into one resulted string
concat :- Concatenate means joining two strings . In C# there is a in-built method for concat two string is concat() method as shown in below code example.
internal class Program
{
static void Main(string[] args)
{
string firstString = "This is .Net Blog ";
string secondString = "and you are reading string opeartions";
string combinedString=string.Concat(firstString, secondString);
Console.WriteLine("first String is:-" +firstString);
Console.WriteLine("second String is:-" + secondString);
Console.WriteLine("After concatenating of firstString and secondString
below is the result :-" );
Console.WriteLine(combinedString);
Console.ReadKey();
}
}
Output of Program :-
first String is:- This is .Net Blog
second String is:- and you are reading string opeartions
After concatenating of firstString and secondString below is the result :-
This is .Net Blog and you are reading string opeartions
In the above program we have used two string variables that is firstString and secondString and we joined this two strings using the string.Concat(firstString, secondString) and assign the result into third string variable named combinedString.
Equals :- Comapring two strings
Equals :- Comapre means checking equality of two strings whether they are matched or not . In C# there is a in-built method for comapre two string is Equals() method and it returns a boolean value either true or false as shown in below code example.
internal class Program
{
static void Main(string[] args)
{
string firstString = "Hello Dear ";
string secondString = "Good Morning";
string thirdString = "Good Morning";
Boolean comapreFirstandSecond = firstString.Equals(secondString);
Console.WriteLine("Compare of First and Second string :-"+
comapreFirstandSecond);
Boolean comapreSecondAndThird = secondString.Equals(thirdString);
Console.WriteLine("Compare of Second and third string :-" +
comapreSecondAndThird);
Console.ReadKey();
}
}
Output of Program :-
Compare of First and Second string :- False
Compare of Second and third string :- True
In the above program we have used three string variables that is firstString, secondString and thirdString. We
comapre first string with second and comapare second string with third string using the Equals() and assign
the result into comapreFirstandSecond and comapreSecondAndThird boolean variables.
First matching is false and second matching is true as expected.
Length :- Getting the length of strings
Length :- In most of the cases we required the Length of a given string in that case we can get it by Length property and it will return a integer value for a string.
internal class Program
{
static void Main(string[] args)
{
string empAddress = "203 street number 23 Mumbai";
int length_of_string = empAddress.Length;
Console.WriteLine("Length of the empAddress string is :- " +
length_of_string);
Console.ReadKey();
}
}
Output of Program :-
Length of the empAddress string is :- 27
In the above program we get the length of the given string using the Length property and assign
the result into a integer variable named as length_of_string.
Length is return the integer value for string length.
Contains :- Checking if a substring is present or not
Contains :- Contains is a in-built method of string in which we can check whether a part of string or sub-string is present into the string or not. Contains() method always return a boolean value either result is false or true.
internal class Program
{
static void Main(string[] args)
{
string empAddress = "203 street number 23 Mumbai";
string searchText = "street number";
Console.WriteLine("Result of search text contains in string :- " +
empAddress.Contains(searchText));
Console.ReadKey();
}
}
Output of Program :-
Result of search text contains in string :- True
In the above program we checked for whether sub string present or not in the given string
using the Contains() method and displays the result on console screen as shown above in
output of the program.
Contains() is return the boolean value in result.
Insert :- Insert a substring in a given string
Insert :- Insert is a in-built method of string in which we can Insert a sub-string at specified position in other string using Insert() method. Insert() method accepts two parameter first one is integer used for index position and second is the string parameter that is sub-string to be inserted.
internal class Program
{
static void Main(string[] args)
{
string introString = "Hi Good morning ,I would like to introduce myself";
string insertionString = "Friends";
string resultedString=introString.Insert(16, insertionString);
Console.WriteLine("After Insertion :-"+ resultedString);
Console.ReadKey();
}
}
Output of Program :-
Hi Good morning Friends,I would like to introduce myself
In the above program we have inserted a sub-string "Friends" in the given string
using the Insert() method and it displays the result on console screen as shown above in
output of the program.
Insert() always insert a sub-string at specified index position in the given string.
Substring :- Extracting a part of string from a given string
Substring :- Substring is a in-built method of string in which we can extract a sub-string from other string using Substring() method. Substring() method accepts two parameter first one is integer used for index position and second is the string parameter that is sub-string to be inserted.
internal class Program
{
static void Main(string[] args)
{
string introString = "Hi Good morning Friends,I would like to introduce myself";
string resultedSubString = introString.Substring(16,21);
Console.WriteLine("Extracted Substring :-"+ resultedSubString);
Console.ReadKey();
}
}
Output of Program :-
Friends,I would like
In the above program we have extracted a sub-string starting from index position 16 and length of substring
should be 21 so it will extract it from index 16 to 37 from the given string
using the Substring() method and it displays the result on console screen as shown above in
output of the program.
Substring() always returns a string from specified index to till string length.
string has many functions from which I have disscussed few functions rest of listed below, I am not going to disscuss on all the functions.
About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the
applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server,
EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.