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.
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.
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