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 3)

Author: Zunnair
Download Source Code : 829_string3.zip

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

 In part1 and part2 some string operations has been discussed. For good understanding please read part1 and part2 of this article.

STRING CONTAINS:

You can check that one string exist in the second or not.

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

Write strings in text boxes and than press button to check.

 Now write the following code on button click event:

C#

private void btn_contain_Click(object sender, EventArgs e)

        {

            string str = txt_stirng.Text;

            string strfor = string.Empty;

            strfor = txt_contain.Text;

            if (str != string.Empty || strfor != string.Empty)

            {

                if ((str.Contains(strfor)) == true)

                {

                    MessageBox.Show("TURE  " + "STIRNG EXIST");

 

                }

                else

                    MessageBox.Show("FALSE  " + "STRING NOT EXIST");

            }

            else

                MessageBox.Show("ENTER STRING THAN PRESS BUTTON");

        }

VB

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

        Dim str As String = txt_stirng.Text

        Dim strfor As String = String.Empty

        strfor = txt_contain.Text

        If str <> String.Empty OrElse strfor <> String.Empty Then

            If (str.Contains(strfor)) = True Then

                MessageBox.Show("TURE " & "STIRNG EXIST")

 

            Else

                MessageBox.Show("FALSE " & "STRING NOT EXIST")

            End If

        Else

            MessageBox.Show("ENTER STRING THAN PRESS BUTTON")

        End If

    End Sub

STRING REMOVE:

You can remove string from a specific index.

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

Write starting index in text box and than press button to remove.

 Now write the following code on button click event:

 

C#

private void btn_Remove_Click(object sender, EventArgs e)

        {

            string str = txt_stirng.Text.Trim();

            int index = 0;

            index = Convert.ToInt16(txtRemove.Text);

            if (index >= 0 && index < (str.Length))

            {

                string strToRemove = str.Remove(index);

                MessageBox.Show("STRING AFTER REMOVING " + strToRemove);

            }

            else

                MessageBox.Show("INVALID INDEX"); 

        }

VB

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

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

        Dim index As Integer = 0

        index = Convert.ToInt16(txtRemove.Text)

        If index >= 0 AndAlso index < (str.Length) Then

            Dim strToRemove As String = str.Remove(index)

            MessageBox.Show("STRING AFTER REMOVING " & strToRemove)

        Else

            MessageBox.Show("INVALID INDEX")

        End If

 

    End Sub

There is also an overloaded function in which you can also tell that how much charactors you want to delete from the starting index.

 

REPLCE STRING:

You can also replace string in a string.

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

Enter main string, old string, and new string than press the button.

Now write the following code on button click event:

C#

private void btnReplace_Click(object sender, EventArgs e)

        {

            string str = txt_stirng.Text.Trim();

            string old = txtOld.Text;

            string n = txtNew.Text;

            if ((str.Contains(old)) == true)

            {

                string modified=str.Replace(old, n);

                MessageBox.Show("New string is "+ modified);

            }

            else

                MessageBox.Show("old string not found");

        }

VB

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

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

        Dim old As String = txtOld.Text

        Dim n As String = txtNew.Text

        If (str.Contains(old)) = True Then

            Dim modified As String = str.Replace(old, n)

            MessageBox.Show("New string is " & modified)

        Else

            MessageBox.Show("old string not found")

        End If

    End Sub

 

TO LOWER:

You can convert the string’s capital letters in small letters.

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

Now write the following code on button click event:

C#

private void btnLowerCase_Click(object sender, EventArgs e)

        {

            string str = txt_stirng.Text.Trim();

            string str_LowerCase = str.ToLower();

           txtLowerCase.Text = str_LowerCase;

        }

VB

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

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

        Dim str_LowerCase As String = str.ToLower()

        txtLowerCase.Text = str_LowerCase

    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 :
 
   
<< String operations in C# .net 2.0. (Part 2)

Disclaimer - Privacy
© 2002-2017 DevASP.net