Split function in VB.NET

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Split function in VB.NET
Skillful
Hey all coders! :D

Welcome to my new tutorial.

I've seen lots of people having problem or don't understanding how to use the String.Split() command in visual basic.

So i decided to make a very small yet informing tutorial on how to use both the regular String.Split() and also the Regex.Split() function.

What you need:

Visual Basic 2010 Express/Proffesional Edition [Other works fine, this is the one i use]

Alright, so we'll start by declaring a string array. This isn't so hard, all basic coders should know how to complete this task.

You might be confused with the "array", but i'll explain it.

Just declare a string like normal, but add "()" after the name of the string, like this:
Code: Select all
Dim Str() As String
Or you could do it like this, its the same thing:
Code: Select all
Dim Str As String()
Now this string doesn't work like normal 1-dimensional strings. You can't do stuff like:
Code: Select all
Str = "CodenStuff.com"
I will not go in deep on why and how to work with it, this tutorial is about splitting strings.

Now, declare another regular string. Like this:
Code: Select all
Dim Str2 As String = "Hello-CodenStuff-!"
The String.Split() command is used to seperate a string into different parts. You can split the string by entering a char in the brackets, like this:
Code: Select all
String.Split("-")
You will need the double quotes as normal, unless you use the Chr or just enter a integer in the brackets, but if this confuses you just type the char to split with into the brackets with double quotes.

You see i putted "-" in Str2, this is to show you an easy example of splitting.

Alright, lets start by typing this:
Code: Select all
Str = Str2.Split("-")

And lets break it up

"Str ="

Just assigns the split to the string array "Str"

Str2.Split("-") splits "Hello-CodenStuff-!" with the character "-".

When you split a string into an array, then for each splitted item, it will add it to an seperate array number. NOTE: All arrays starts with 0 and not 1.

So Str(0) is now "Hello". You might think "What the heck? How?!". Well, its pretty simple when you think about it. As i said all splitted items gets their own number. The word before the first "-" (Or whatever char you used to split) is assigned the first array number. Which is 0.

So if Str(0) is Hello and Str(1) is CodenStuff, what can Str(2) be?

Thats right, its "!".

Now i think you got the general idea of the regular splitting :)

Thing is, when you split with "-" it doesn't show that char. So this:
Code: Select all
Msgbox(Str(0) & " " & str(1) & str(2))
Will show "Hello CodenStuff!"

Now what if you haev a string like this?:
Code: Select all
Dim Str2 As String = "Hello[abc]CodenStuff[abc]!"
Then the String.Split() can unfortunaly not split by more than 1 char. So if you'd try to split this string with example "[" it will look very ugly and weird.

So now we need Regex.Split()

To use Regex, start by importing
Code: Select all
Imports System.Text.RegularExpressions
Then lets get going! :D

Alright, so lets make Str2 this:
Code: Select all
Str2 = "Hello[abc]CodenStuff[abc]!"
So, regex kinda works the same way as the normal split does, but it allows you to use it differently, for example you can split strings by patterns (wich is essentially a word or sentence or a bunch of chars instead of just one).

So lets do the same as before, but now using Regex.
NOTE: The syntax of regex split is:

Regex.Split(Input, Pattern, Options)

We'll nevermind the options part and just use Input and Pattern.

Alright, the pattern in that we wanna split in this string is "[abc]" right? And whats the input?
NOTE: Input is the string you wanna split, can be a textbox or something also.

So input is Str2 And Pattern is "[abc]"

So now we'll assign the splitted string to Str().
Code: Select all
Str = Regex.Split(Str, "[abc]")
Now Str(0) is "Hello", Str(1) is "CodenStuff" and Str(2) is "!".

There you go! :D

Now you've splitted a string with both regex and regular splitting. Congratulations!

Some remember notes:

Arrays always starts with 0 and not 1 as alot of people new to programming assumes.

The first word/sentence/text before the first split char/pattern is assigned the array number "0". The sentence after the char/pattern is assigned 1, then 2 etc.

Have fun and good luck with your programming!


Hope you liked my tutorial.


Regards,
Skillful
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Split function in VB.NET
MrAksel
Skillful wrote:
Now this string doesn't work like normal 1-dimensional strings. You can't do stuff like:
Code: Select all
Str = "CodenStuff.com"
"CodenStuff.com" is not a 1-dimensional string. The array you created is 1-dimensional :)

Nice that you cover some regex too cooll;
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Re: Split function in VB.NET
Skillful
Thanks for your positive feedback MrAksel. cooll;
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
3 posts Page 1 of 1
Return to “Tutorials”