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

Author: Zunnair
Download Source Code : 828_string2.zip

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

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

STRING CONCATINATION:

You can concatenate two strings.

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

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

 Now write the following code on button click event:

C#

private void btn_concatenate_Click(object sender, EventArgs e)

        {

            string strToConcatenate = string.Empty;

            strToConcatenate = txt_concatenate.Text;

            str = txt_stirng.Text;

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

            {

                 MessageBox.Show(str + strToConcatenate);

 

            }

            else

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

        }

VB

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

        Dim strToConcatenate As String = String.Empty

        strToConcatenate = txt_concatenate.Text

        str = txt_stirng.Text

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

            MessageBox.Show(str + strToConcatenate)

 

        Else

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

        End If

    End Sub

STRING COMPARE:

You can compare two strings.

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

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

 Now write the following code on button click event:

 

C#

private void btn_compare_Click(object sender, EventArgs e)

        {

            str=txt_stirng.Text;

            string strforcompare = string.Empty;

            strforcompare = txt_compare.Text;

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

            {

                if ((str.CompareTo(strforcompare))==0)

                {

                    MessageBox.Show("TURE  " + "BOTH STIRNGS ARE SAME");

 

                }

                else

                    MessageBox.Show("FALSE  " + "BOTH STRINGS ARE NOT SAME");

            }

            else

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

        }

VB

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

        str = txt_stirng.Text

        Dim strforcompare As String = String.Empty

        strforcompare = txt_compare.Text

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

            If (str.CompareTo(strforcompare)) = 0 Then

                MessageBox.Show("TURE " & "BOTH STIRNGS ARE SAME")

 

            Else

                MessageBox.Show("FALSE " & "BOTH STRINGS ARE NOT SAME")

            End If

        Else

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

        End If

    End Sub

INSERT STRING:

You can also insert string anywhere in the second 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 btnInsert_Click(object sender, EventArgs e)

        {

            string str = txt_stirng.Text.Trim();

            string strToInsert = txtInsert.Text.Trim();

 

            string newString = str.Insert(0, strToInsert);

 

            MessageBox.Show("New STRING is : " + newString);

 

        }

VB

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

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

        Dim strToInsert As String = txtInsert.Text.Trim()

 

        Dim newString As String = str.Insert(0, strToInsert)

 

        MessageBox.Show("New STRING is : " & newString)

 

    End Sub

 

TO UPPER:

You can convert the string’s small letters in upper 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 btnUpperCase_Click(object sender, EventArgs e)

        {

            string str = txt_stirng.Text.Trim();

            string str_UpperCase = str.ToUpper();

            txtUpperCase.Text = str_UpperCase;

        }

VB

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

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

        Dim str_UpperCase As String = str.ToUpper()

        txtUpperCase.Text = str_UpperCase

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

Disclaimer - Privacy
© 2002-2017 DevASP.net