[Explanation] Proper use of Split

Discussion in 'Archives' started by Puzzle, Mar 1, 2007.

[Explanation] Proper use of Split
  1. Unread #1 - Mar 1, 2007 at 4:59 PM
  2. Puzzle
    Joined:
    May 6, 2005
    Posts:
    846
    Referrals:
    0
    Sythe Gold:
    0

    Puzzle Apprentice

    [Explanation] Proper use of Split

    Ok, in this tutorial we will be focusing on how to split data into different pieces so you can easily parse them into labels, textboxes or whatever you want.

    Requirements -
    1. Patience ( this is a easy function to use but hard at first to understand )
    2. Intermediate Knowledge of Visual Basic

    What we will be doing - Learning how to split a simple string

    Ok lets get started

    1. Open up vb and setup 1 textbox, 1 label and 1 command button. Label them whatever you want, just dont change the names for the sake of this tutorial.

    2. Now lets study how a split function works

    the split function has 4 parts, i will not name the vb words for them but instead define them myself since its easier to understand that way. Also for anyone with past split experience, i will only be using the first 2 parts of the function and ignoring the last 2.

    So now lets explore how the split function works.

    In a normal split, this is how you would remember the function.

    Code:
     Split(stringname, "what to split by") 
    now study that well and remember the string name is NEVER put into quotations and what your splitting by will ALWAYS be in quotes.

    3. Now after you have studied the format of the split function lets move onto writing a simple piece of code.

    onto your labels caption write in " Puzzle owns hehe " ( no quotes )

    now double click your button and lets write some code.
    Whenever you begin a split you should always create something i call the BASE string. This will be the string that all other following splits will split with.
    Lets create that now, type in the following code

    Code:
    Dim base as string
    base = label1.caption
    What we did was we created a string ( which is a piece of data that contains what the programmer specifies ). We also specified that the base is label1's caption. Meaning anything in label1 will be the base string.

    4. Now lets create our split strings. When creating a split string you must remember that these strings are going to be containing an array ( ill explain what an array is later ). So when you create a split string you must always have a () following the string name.

    So double click your command button and add in this code right after the previous code.

    Code:
    Dim split0() as string
    Now you see what i mean? you must always have () just to specify that it will contain data. Also i used 0 instead of 1 because in vb mostly anything that involves counting starts at 0. Just keep this in mind for later, it will come useful as you do more complex splitting.

    5. Now that we have our strings set up, lets do our first split! Now you must recall the format of the split function. Heres the code you will put into your command button right under the previous code.

    Code:
    split0() = split(update, "owns")
    now let me explain what this does before you run it and see for yourself. Basically in this line of code we are expressing that we want to split the string update which is label1's caption "Puzzle owns hehe". We are splitting the string update into 2 pieces, and those 2 pieces are whatevers BEFORE owns and whats AFTER owns.

    After = hehe
    Before = Puzzle

    See?

    6. Now the last step of a split is placing the function INTO something. Thats where our textbox comes into play. right after the previous code add

    Code:
    text1.text = split0(0)
    What we are saying is that we want the textbox to show whats BEFORE owns. Why before? thats where the (0) comes in. When you specify a split function, vb will automatically assign the splitted parts of your code numbers. So usually in a simple split like this 0 is given to whats BEFORE owns and thats "Puzzle" and 1 is given to "hehe".

    So if you run that code your textbox should say Puzzle, now if you changed
    it to
    Code:
    text1.text = split0(1)
    what do you think will appear? You should be able to answer that by now :)

    Have fun guys :p
     
< -How to Stop Reboots- | -How to Get a Free Domain- >

Users viewing this thread
1 guest


 
 
Adblock breaks this site