Substring in Visual Basic .Net

Download
Download is available until [expire_date]
  • Version
  • Download 68
  • File Size 104.52 KB
  • File Count 1
  • Create Date April 29, 2016
  • Last Updated April 29, 2016

Substring in Visual Basic .Net

Substring in Visual Basic .Net

The substring will get a portion from the source string to create a new one.

Mystring.substring(startIndex, lengthspecified)

Substring function parameter:

startIndex is where the new string (substring) will start
lengthspecified is how many characters to be displayed starting from the startIndex value.

Example:
String = “ Hello world”
String.substring(1,3)
Result would be:
ell
note: index starts in 0

Source code:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mystring As String = TextBox1.Text

        If TextBox1.Text = "" Then
            MessageBox.Show("Please enter a text on the textbox")
            TextBox1.Focus()
        Else
            If IsNumeric(TextBox2.Text) = False Or TextBox2.Text = "" Then
                MessageBox.Show("Please enter start index")
                TextBox2.Focus()
            ElseIf IsNumeric(TextBox3.Text) = False Or TextBox3.Text = "" Then
                MessageBox.Show("Please enter length of the substring")
                TextBox3.Focus()
                TextBox3.Text = mystring.Length
            ElseIf Val(TextBox2.Text) >= Len(mystring) Or Val(TextBox3.Text) >= Len(mystring) Then
                MessageBox.Show("the value of the start index and length of the substring must not exceed to the length of the text entered")
            Else
                MessageBox.Show(mystring.Substring(TextBox2.Text, TextBox3.Text))

            End If

        End If

    End Sub
End Class
, , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.