X



Home>Tutorial>

Visual Basic Split Function Detailed Example

Nuelson Penuel Thursday, September 10, 2020 Visual Basic.Net

Split Function Overview

The Split() function in Visual Basic is used to extract a substrings from a given string. Before a split function can work in a string, there must be a delimiter that separate each of the substrings from each other. A delimiter can be anything you choose e.g semi-colon, comma, space, words or any other special characters. Assuming you have a string as "You can ask all your programming questions here on emmason247.com.ng", and you want to split it. All you have to do is either use the single space as your delimiter or replace single space with "-" and it will now be "You-can-ask-all-your-programming-questions-here-on-emmason247.com.ng" However, if the separator parameter is null or contains no characters, white space characters are assumed to be the delimiters.

Split() Function Syntax


Strings.Split(Expression, Delimiter, Limit, Compare) 
Parameters Description
  • Expression: String expression containing substrings and delimiters.
  • Delimiter: Any single character used to identify substring limits. If Delimiter is omitted, the space character (" ") is assumed to be the delimiter.
  • Limit: Maximum number of substrings into which the input string should be split. The default, -1, indicates that the input string should be split at every occurrence of the Delimiter string.
  • Compare: Numeric value indicating the comparison to use when evaluating substrings.

Split() Function Example

Below is a simple example of Split function
 
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String ="You can ask all your programming questions here on emmason247.com.ng"
        Dim strArr() As String
        strArr = str.Split(" ")
'for us to get the splitted items
'use for to next statement
        For x = 0 To strArr.Length - 1
            MsgBox(strArr(x))
        Next
    End Sub
End Class
And the result will be
You 
can 
ask 
all 
your 
programming
questions
here 
on 
emmason247.com.ng
In the above example, the splitting was done using single space. However, what happens if the string has no white space like this "You-can-ask-all-your-programming-questions-here-on-emmason247.com.ng". Let us try the above example but this time with a string with special character.
 
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As String ="You-can-ask-all-your-programming-questions-here-on-emmason247.com.ng"
        Dim strArr() As String
        strArr = str.Split("-")
'for us to get the splitted items
'use for to next statement
        For x = 0 To strArr.Length - 1
            MsgBox(strArr(x))
        Next
    End Sub
End Class
And the result will be
You 
can 
ask 
all 
your 
programming
questions
here 
on 
emmason247.com.ng

| | Views(1215)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services