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
 

Get length, string and substring of a string by using “stringInfo” class in vb and C# .net.

Author: Maliha Atteeq
Download Source Code : 1056_stringApplications.zip

In this simple article you will know how we can get length, string and substring of a string by using “stringInfo” class in vb and C #.net.

 String info class:

System.globalization.string info class is used to split a string into text elements and to iterate through those text elements.

To get length of text element:

In order to get length of the text element, stringInfo class provides us a property named as LengthInTextElements. The return type of this property is integer. It returns the length of the text element in the form of integer.

To demonstrate make a new window application. Drag one button on the form and write the following code:

C#:

private void btn_getLength_Click(object sender, EventArgs e)

        {

            int length;

            length=obj.LengthInTextElements;

            MessageBox.Show("the length of text element is "+Convert.ToInt16(length));

        }

VB:

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

    Dim length As Integer

    length = obj.LengthInTextElements

    MessageBox.Show("the length of text element is " & Convert.ToInt16(length))

End Sub

To get/set string:

In order to get or set the string of the object, stringInfo class provides us a property named as String.

To get the string:

 The return type of this property is string. It returns the string passed in the constructor of the string info class.

To demonstrate make a new window application. Drag one button on the form and write the following code:

C#:

private void btn_getstr_Click(object sender, EventArgs e)

        {

            string str;

            str=obj.String;

            MessageBox.Show("the string is " + str);

        }

VB:

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

    Dim str As String

    str = obj.[String]

    MessageBox.Show("the string is " & str)

End Sub

To set the string:

In order to set a string, we assign a string value to this property. The string assigned to it will set as the string of the object.

To demonstrate make a new window application. Drag one button and one text box on the form and write the following code:

C#:

private void btn_setStr_Click(object sender, EventArgs e)

        {

            string str;

            str = txt_setStr.Text;

            obj.String = str;

            MessageBox.Show("the string is set to "+str);

        }

VB:

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

    Dim str As String

    str = txt_setStr.Text

    obj.[String] = str

    MessageBox.Show("the string is set to " & str)

End Sub

To set the substring:

In order to get a substring, this class provides us a function named as SubstringByTextElements(). This function takes two argument as a parameter, one is the starting index and the second is the count. The return type of this function is string. It returns the string starting from the starting index upto the count.

To demonstrate make a new window application. Drag one button and two text boxes on the form and write the following code:

C#:

private void btn_substring_Click(object sender, EventArgs e)

        {

            int startingIndex;

            int count;

            string str;

            startingIndex = Convert.ToInt16(txt_startIndex.Text);

            count = Convert.ToInt16(txt_count.Text);

            str=obj.SubstringByTextElements(startingIndex,count);

            MessageBox.Show("the substring is "+str);

        }

VB:

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

    Dim startingIndex As Integer

    Dim count As Integer

    Dim str As String

    startingIndex = Convert.ToInt16(txt_startIndex.Text)

    count = Convert.ToInt16(txt_count.Text)

    str = obj.SubstringByTextElements(startingIndex, count)

    MessageBox.Show("the substring is " & str)

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP STRING APPLICATION";

        }

VB:

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

   Me.Text = "DEVASP STRING APPLICATION"

End Sub

This simple article tells how we can get length, string and substring of a string by using “stringInfo” class in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Write, close and dispose string by using “string writer” class in vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net