Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

String operations in C# .net 2.0. (Part 1)

Author: Zunnair
Download Source Code : 827_string1.zip

In this simple article you will learn how to use and manipulate strings in C# .net 2.0.

 Now write the following code.

C#

string str;

VB

Dim str As String

It is a normal practice that string should be initializing with null or it should be empty.

FIRST WAY TO INITIALIZE EMPTY:

Now write the following code.

C#

string str = "";

VB

Dim str As String = ""

This is not a recommended method to declare string empty.

Dot Net provide us a built in facility to initialize a string as empty.

SECOND WAY TO INITIALIZE EMPTY:

Now write the following code.

C#

string str = string.Empty;

VB

Dim str As String = String.Empty

 

This is a recommended way to initialize a string as empty.

GETTING INPUT:

You can get the input from text box.

Now write the following code.

C#

Str = TextBox1.Text;

VB

Str = TextBox1.Text

 

It is a property of text box that it returns a string so we can store it in a variable.

This code will get the string from the text box and will store it in string variable (str).

GETTING LENGTH OF STRING:

You can get the length of the string entered.

To demonstrate this drag one text box control and one button on form.

 Now write the following code on button click event:

C#

private void btnGetStringLength_Click(object sender, EventArgs e)

        {                   

            string str = textBox1.Text;

            int Length = str.Length;

            MessageBox.Show(Length.ToString());     

        }

VB

Private Sub btnGetStringLength_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = textBox1.Text

        Dim Length As Integer = str.Length

        MessageBox.Show(Length.ToString())

    End Sub

REMOVING EXTRA SPAECS:

Some time we have a risk that user can enter extra spaces. To control this problem Dot Net provides us a built in facility.

Now write the following code.

 

C#

string str = textBox1.Text.Trim();

VB

Dim str As String = textBox1.Text.Trim()

STIRNG ENDS WITH:

You can also check that the string ends with particular string or not.

To demonstrate this drag two text boxes and one button on form.

Now write the following code on button click event:

C#

private void btnEndsWith_Click(object sender, EventArgs e)

 {

 

   string str = textBox1.Text.Trim();

   string strToCompare = textBox2.Text.Trim();

 

   if (str.EndsWith(strToCompare))

   {

     MessageBox.Show("True  " + str + " Ends With " + strToCompare);

   }

  else

   {

     MessageBox.Show("False " + str + " Does not Ends With " + strToCompare);      

   }

}

VB

Private Sub btnEndsWith_Click(ByVal sender As Object, ByVal e As EventArgs)

 

   Dim str As String = textBox1.Text.Trim()

   Dim strToCompare As String = textBox2.Text.Trim()

 

   If str.EndsWith(strToCompare) Then

   MessageBox.Show(("True " & str & " Ends With ") + strToCompare)

   Else

   MessageBox.Show(("False " & str & " Does not Ends With ") + strToCompare)

        End If

    End Sub

GETTING INDEX VALUE:

You can also get the index of the first occurrence of a particular character which exists in the string.

To demonstrate this drag two text boxes and one button on form.

Now write the following code on button click event:

C#

private void btnIndexOf_Click(object sender, EventArgs e)

 {        

    string str = textBox1.Text.Trim();

    string strToCheck = textBox2.Text.Trim();

    int index = str.IndexOf(strToCheck);

 

    if (index == -1)

    {

      MessageBox.Show("False - Enetred string does not exist ");

    }

    else

    {

MessageBox.Show("True - Enetred string exist at position : " + index.ToString());

    }     

 }

VB

Private Sub btnIndexOf_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim str As String = textBox1.Text.Trim()

        Dim strToCheck As String = textBox2.Text.Trim()

        Dim index As Integer = str.IndexOf(strToCheck)

 

        If index = -1 Then

            MessageBox.Show("False - Enetred string does not exist ")

        Else

            MessageBox.Show("True - Enetred string exist at position : " & index.ToString())

        End If

    End Sub

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP.NET STRING APPLICATION";

        }

 

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP.NET STRING APPLICATION"

    End Sub

 

This is the simple article related to string operations.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with ShowDialog and Show Function of window form in .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net