locked
Splitting a string and putting each split in to an array. RRS feed

  • Question

  • Wadup!

    I have been inspired by Iron Man 3 and Jarvis :)
    I wanted to make some kind of controlcentre of my own. I want to be able to write commands in to a richtextbox in the program, and then the program should do stuff for me.

    I didn't want the commands to be so very strict. I didn't want something like:

    Dim command As String

    command = richtextbox1.text          (I have written my command in to the richtextbox)

    If command = "Open Internet explorer" Then

    OpenExplorer()

    End If

    If i did it like that, it would be enough with me saying "Open IE" for the program to fail... So i had another thought.

    What if i took the String with my command and divided it in to several parts, where every part would represent a word. Then i check if command had any of the words: "IE" or "Internet" or "Internet Explorer" together with "Open" or "Launch" or "Start", it would start.

    (here's question 1): So how do you split a string into several different strings? And then even more interesting, (here's question 2): how do i put every part of that string in to a variable? I guess it's smart to use an array for that, and not make a shitload of normal variables.

    And then, the last question. When i have an array, how do i check if the word "IE" exists somwhere in the array?

    I MIGHT HAVE PLACED THIS QUESTION IN THE WRONG CATEGORY. PLEASE NOTIFY ME IF THAT IS THE CASE :)


    How do i write my signature on the computer?

    Tuesday, June 18, 2013 8:59 PM

Answers

  • [...] question 1): So how do you split a string into several different strings?

    Dim Parts as String() = command.Split(" "c);

    This also answers your question 2, since it provides an array of parts.

    And then, the last question. When i have an array, how do i check if the word "IE" exists somwhere in the array?

    Dim containsIE as Boolean = Parts.Contains("IE")

    I MIGHT HAVE PLACED THIS QUESTION IN THE WRONG CATEGORY. PLEASE NOTIFY ME IF THAT IS THE CASE :)

    Yes, it is wrong to place this question in the Training and Certification forum. I suggest the VB.NET forum in MSDN:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

    • Marked as answer by OptiGE Wednesday, June 19, 2013 11:56 AM
    Wednesday, June 19, 2013 4:34 AM

All replies

  • [...] question 1): So how do you split a string into several different strings?

    Dim Parts as String() = command.Split(" "c);

    This also answers your question 2, since it provides an array of parts.

    And then, the last question. When i have an array, how do i check if the word "IE" exists somwhere in the array?

    Dim containsIE as Boolean = Parts.Contains("IE")

    I MIGHT HAVE PLACED THIS QUESTION IN THE WRONG CATEGORY. PLEASE NOTIFY ME IF THAT IS THE CASE :)

    Yes, it is wrong to place this question in the Training and Certification forum. I suggest the VB.NET forum in MSDN:

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral

    • Marked as answer by OptiGE Wednesday, June 19, 2013 11:56 AM
    Wednesday, June 19, 2013 4:34 AM
  • The ";" sign you put after your first line, is that needed or are you just damaged from some other language? :)

    How do i write my signature on the computer?

    Wednesday, June 19, 2013 12:09 PM
  • The ";" sign you put after your first line, is that needed or are you just damaged from some other language? :)

    Yeah, I've been damaged from another language :-) .  Unfortunately after you have been throwing code at your computer in one language for a few hours, your fingers tend to spew all by themselves the same symbols that you were typing before. Just about everyone I know who has been typing C# or a similar language for a while tend to add a semicolon at the end of the lines when they shift into VB (and similarly, after typing VB for a while, you forget your semicolons in C).

    Wednesday, June 19, 2013 7:36 PM