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
Hey all coders! 
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:
Now, declare another regular string. Like this:
You see i putted "-" in Str2, this is to show you an easy example of splitting.
Alright, lets start by typing this:
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:
Now what if you haev a string like this?:
So now we need Regex.Split()
To use Regex, start by importing
Alright, so lets make Str2 this:
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().
There you go!
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

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
Or you could do it like this, its the same thing:
Dim Str() As String
Code: Select all
Now this string doesn't work like normal 1-dimensional strings. You can't do stuff like:
Dim Str As String()
Code: Select all
I will not go in deep on why and how to work with it, this tutorial is about splitting strings.Str = "CodenStuff.com"
Now, declare another regular string. Like this:
Code: Select all
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:
Dim Str2 As String = "Hello-CodenStuff-!"
Code: Select all
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.String.Split("-")
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
Will show "Hello CodenStuff!"Msgbox(Str(0) & " " & str(1) & str(2))
Now what if you haev a string like this?:
Code: Select all
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.Dim Str2 As String = "Hello[abc]CodenStuff[abc]!"
So now we need Regex.Split()
To use Regex, start by importing
Code: Select all
Then lets get going! Imports System.Text.RegularExpressions

Alright, so lets make Str2 this:
Code: Select all
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).Str2 = "Hello[abc]CodenStuff[abc]!"
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
Now Str(0) is "Hello", Str(1) is "CodenStuff" and Str(2) is "!".Str = Regex.Split(Str, "[abc]")
There you go!

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!
LSIBMHBIWFETALOL
Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
Skillful wrote:Now this string doesn't work like normal 1-dimensional strings. You can't do stuff like:"CodenStuff.com" is not a 1-dimensional string. The array you created is 1-dimensionalCode: Select allStr = "CodenStuff.com"

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]()
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!

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!
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023