Category Archives: C# String

How to use string Length() in C#

Length()  – The string Length method used to calculate the character length of string.

Length method returns the integer value in result.

 

How to use Length() string method in C#

How to use Length() string method in C#

 

write c# code on button click events :

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = “Length = ” +TextBox1.Text.Length.ToString();
}

 

How to use Length() string method in C#

How to use Length() string method in C#

 

How to use Length() string method in C#

How to use Length() string method in C#

How to use string StartsWith() and EntsWith() in C#

StratsWith() – StartsWith() string method check if the parameter string startswith specified string.

EndsWith() – EndsWith() string method check if the parameter string endswith specified string.

How to use StratsWith() and EndsWith() string method in C#

How to use StratsWith() and EndsWith() string method in C#

 

write below code on Stratswith button click events:

protected void Button1_Click(object sender, EventArgs e)
{
string str1 = TextBox1.Text;
string str2 = TextBox2.Text;

Label1.Text = str1.StartsWith(str2).ToString();
}

How to use StratsWith() and EndsWith() string method in C#

How to use StratsWith() and EndsWith() string method in C#

 

write below code on Endswith button click events:

protected void Button2_Click(object sender, EventArgs e)
{
string str1 = TextBox1.Text;
string str2 = TextBox2.Text;

Label1.Text = str1.EndsWith(str2).ToString();
}

How to use StratsWith() and EndsWith() string method in C#

How to use StratsWith() and EndsWith() string method in C#

 

How to use StratsWith() and EndsWith() string method in C#

How to use StratsWith() and EndsWith() string method in C#

string ToUpper() and ToLower() in C#

ToUpper() – ToUpper() string method in c# used to convert string alphabets values to Uppercase alphabets values.

ToLower() – ToLower() string method in c# used to convert string alphabets values to Lowercase alphabets values.

Here, we understand ToUpper() and Tolower() method with an example.

How to use ToUpper() and ToLower() string method in C#

How to use ToUpper() and ToLower() string method in C#

write below code on ToUpper() Button control click events:

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text.ToUpper();

}

code for ToLower() button control:

 protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text.ToLower();
}

 

When we click the button ToUpper, all the values of textbox will be converted to Uppercase, shows like below screen.

How to use ToUpper() and ToLower() string method in C#

How to use ToUpper() and ToLower() string method in C#

 

How to use ToUpper() and ToLower() string method in C#

How to use ToUpper() and ToLower() string method in C#

How to use string Contains method in C#

The string contains method is check if specified character is found in string or not.

The string contains method returns true/false, if string found in content of specified string then returns true other wise returns false.

Here, we tack an example to understand the concept of string contains method.

Here, we have two string str1 and str2, we check if the str2 value found in str1 the the result will be true, if str2 content is not found in str1 the result will be false.

First, design the asp.net web forms with two textbox control, a Button and a label control.
Here, we check if the content of string str2 is found/match with string str1 content then result will be true, if not then result will be false.

How to use string contains in C#

How to use string contains in C#

write below code on button control click event:

protected void Button1_Click(object sender, EventArgs e)
{
string str1=TextBox1.Text;
string str2 =TextBox2.Text;
Label1.Text = str1.Contains(str2).ToString();
}

we can see here the string1 value “Meera” and string2 value “Me”, the string2 value fond in the string1, so result is true.

How to use string contains in C#

How to use string contains in C#

Here, string2 value is not found in string1, so result is false.

How to use string contains in C#

How to use string contains in C#

How to use string Compare method in C#

We use string Compare function for comparison of two string length.  In string compare function we get three types of out put in result. suppose we have two string str1 and str2, we apply string compare function on string then we get result like both string are same, str1 is greater than str2, str2 is greater than str1.

Here, we can get result in integer value Zero, grater than Zero, less than Zero

If both string are same =output result is Zero

str1 is greater than str2 = output result is grater than Zero

str2 is grater than str1 = output result is less than Zero

lets, take an example of string compare.

Create asp.net web application and design a web forma with two textbox, one Button along with a Label control.

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

write below code on button click event.

C# Code for string compare

 protected void Button1_Click(object sender, EventArgs e)
{
string str1=TextBox1.Text;
string str2=TextBox2.Text;
int result = string.Compare(str1, str2);
Label1.Text =”Result = “+ result.ToString();
}

VB.Net Code

Dim str1 As String = TextBox1.Text
Dim str2 As String = TextBox2.Text
Dim result As Integer =  String.Compare(str1,str2)
Label1.Text =”Result = “+ result.ToString()

 

In below screen the both str1 and str2 are same, so result would be Zero.

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

 

Here, str1 is grater than str2 then result would be grater than Zero means result is 1

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

 

In below screen str2 is grater than str1 so result is -1.

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

How to use string Clone() and Copy() in C#

The string clone() and copy() both method are used to copy purpose.

Clone() – The Clone() method used to copy the structure of the actual data, or we can say copy the reference of the instance of string.
– If we use clone() method for copy DataTable then it returns the copy of structure of all DataTable, but it doesn’t contain any DataRows.

Copy() – The Copy() method create a same new string object with the same content. The Copy() method in c# used to copy the structure of data as well as actual data.
– If we use copy() method for copy DataTable then it returns the copy of structure of all DataTable with same set of  DataRows as original table.

Example of Clone() method in C#

    protected void Page_Load(object sender, EventArgs e)
{
string str = “Example of Clone() Method”;
string strclone = null;
strclone = (String)str.Clone();
Label1.Text = strclone;
}

Clone() method in VB.Net Code

 Dim str As String = “Example of Clone() Method”
Dim strclone As String = Nothing
strclone = CType(str.Clone(), String)
Label1.Text = strclone

 

Example of Copy() method in C#

 string str1 = null;
string str2 = null;

str1 = “Example of Copy() Method”;
str2 = string.Copy(str1);
Label1.Text = str2;

Copy() method in VB.Net

 Dim str1 As String = Nothing
Dim str2 As String = Nothing

str1 = “Example of Copy() Method”
str2 = String.Copy(str1)
Label1.Text = str2

In above copy() method example when we run the program we get same string result in str2 and str1.