Split string in list vb net

Two very useful string variable methods are Split and Join. Split[] allows you to split a line of text and put each element [word or phrase] into an array; Join[] allows you to join elements of an array into one line of text. An example or two might clear this up.

The VB NET Split[] Method

In a later project, you'll have to open up text file and read it's contents. You can read the text file line by line, and each line might be something like this:

"UserName1, Password1, UserName2, Password2, UserName3, Password3"

The programming problem is to separate each word. You can use Split for this. Each word would then be separated, ready for you to place into an array.

Here's an example for you to try out. [It's better to put this code behind a new button]:

Dim LineOfText As String
Dim i As Integer
Dim aryTextFile[] As String

LineOfText = "UserName1, Password1, UserName2, Password2"

aryTextFile = LineOfText.Split[ "," ]

For i = 0 To UBound[aryTextFile]

MessageBox.Show[ aryTextFile[i] ]

Next i

Notice the line that sets up an array:

Dim aryTextFile[] As String

We don't know how many elements will be in the array [how many words on each line], so we leave the round brackets blank.

The next line just put some text into a variable called LineOfText. But this can come from a text file that you open with code.

The line that does the splitting comes next:

aryTextFile = LineOfText.Split[ "," ]

Notice that aryTextFile has now lost its round brackets. If you put them in, you get an error. The use of the Split[] method, though, is this:

LineOfText.Split[ "," ]

After the name of your variable and the full stop, select [or type] the word Split. In between round brackets you put what is know as the separator. This is the symbol or punctuation mark that you are using to separate each element of your line. In our case, we're using a comma to separate the words in the line. But you can use anything you like [a hyphen, for example].

When VB finishes the splitting, it fills up your array. Each element will occupy one slot in your array. So in our example, aryTextFile[0] will hold a value of UserName1, aryTextFile[1] will hold a value of Password1, etc.

The For loop is there to show you how to loop round each element in your array, and displays the results in a message box:

For i = 0 To UBound[ aryTextFile ]

MessageBox.Show[ aryTextFile[i] ]

Next i

The first line includes this:

UBound[ aryTextFile ]

UBound means Upper Boundary of an array. In between the round brackets of UBound[] you type the name of your array. Notice, though that the round brackets of the array have gone missing again.

So if your array was this:

MyArray[9]

The Upper Boundary of the array would be 9. So the end of the For Loop would then be 9. You code like this when you don't know how many elements are in your array.

The message box just displays what is in each position of your array:

MessageBox.Show[ aryTextFile[i] ]

The point is that all of the words from your line of text have been split and placed into an array. You now need to know how to put the text back together again.

The VB NET Join[] Method

The Join method is used when you want to join the elements of an array back together again. Here's some code which does exactly that.

Dim LineOfText As String
Dim i As Integer
Dim aryTextFile[3] As String

aryTextFile[0] = "UserName1"
aryTextFile[1] = "Password1"
aryTextFile[2] = "UserName2"
aryTextFile[3] = "Password2"

LineOfText = String.Join[ "-", aryTextFile ]

MessageBox.Show[ LineOfText ]

The line that joins each element in the array is this:

LineOfText = String.Join[ "-", aryTextFile ]

[NOTE: If you have an older version of the VB NET software, use LineOfText.Join instead of String.Join.]

In between the round brackets of Join[], you first type what you want to use as a separator. Here, we're using an hyphen as a separator. Next, you put the name of your array. Again the round brackets from the array have gone missing.

When the line executes, the variable LineOfText will hold the following:

"UserName1-Password1-UserName2-Password2"

Once you have the array elements joined together, you could then write the line back to your text file.

Split and Join can be very useful indeed. Especially when you're working with text files. Speaking of which, the next section deals with exactly this subject.

Back to the VB NET Contents Page

Video liên quan

Chủ Đề