Results 1 to 10 of 10

Thread: Splitting a string into an array?

  1. #1

    Thread Starter
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171

    Exclamation Splitting a string into an array?

    Hey, all. Having a bit of trouble splitting this string into an array and then displaying it in a message box. Here's my code:

    Code:
    		Dim myString As String = "abcdefg"
    		Dim myArray As Array = myString.Split("")
    		MsgBox(myArray)
    That should, if I'm not mistaken (which I am, because it's not working ) split up the string "myString" and put each character in its own entry of myArray, and then display the entire array in a message box, but that isn't happening. The error I'm getting when I do this is as follows:

    Argument 'Prompt' cannot be converted to type 'String'.
    Also - do you reference a specific entry of an array via the opening and closing brackets like this?: [STRINGNUMBER]


    Thanks!
    Rick
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Your trouble is that you can't display an array in a msgbox like that. The prompt argument of the Msgbox function takes a string so you'd have to show a string or join the array elements back together.

    Msgbox(myArray(0))

  3. #3
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs down

    myString.Split("") doesn't split the text on each character when i try. Upper bound of the array is 0.
    ~Peter


  4. #4

    Thread Starter
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Originally posted by Edneeis
    Your trouble is that you can't display an array in a msgbox like that. The prompt argument of the Msgbox function takes a string so you'd have to show a string or join the array elements back together.

    Msgbox(myArray(0))
    Thanks, Ed.

    Originally posted by Edneeis
    myString.Split("") doesn't split the text on each character when i try. Upper bound of the array is 0.
    I've found that what you said is correct. So the question is, how do you put each character of the string in to its own element in the array?
    Last edited by Fat_N_Furry; Feb 6th, 2003 at 02:21 PM.
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  5. #5
    Member
    Join Date
    Dec 2002
    Location
    NY, USA
    Posts
    52
    Dim s as string = "ABCDE"
    Dim c as Char
    dim arr() as string
    dim i as short=0

    'redim your array to the length of the string

    For Each c In s
    arr(i) = c
    i+=1
    Next
    Iouri Boutchkine

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually a string is just an array of char and you can access that array with the Char property of the string variable.

    VB Code:
    1. msgbox(myString.Char(2))

  7. #7
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Originally posted by Edneeis
    Actually a string is just an array of char and you can access that array with the Char property of the string variable.

    VB Code:
    1. msgbox(myString.Char(2))
    yes what he said, Fat_N_Furry is it really necesary to convert it into an array?

  8. #8

    Thread Starter
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Originally posted by Geespot
    Fat_N_Furry is it really necesary to convert it into an array?

    Yes. I have to split the string into an array and then monkey with it a bit.

    Thanks for your help guys. Got what I needed


    Peace!
    Rick
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

  9. #9
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    Using regular expressions is much easier..

    Code:
    using System.Text.RegularExpressions;
    
    string splitThis = "stuff to split";
    MatchCollection words = Regex.Matches(splitThis, @"\w+");
    
    foreach(Match m in words) {
        Console.WriteLine(m.ToString());
    }
    Take a look at the System.Text.RegularExpressions namespace in your object browser and play around. You can find some tutorials on the web (or probably just do a search in the MSDN docs) on how to make string patterns for the regular expressions.

  10. #10

    Thread Starter
    Addicted Member Fat_N_Furry's Avatar
    Join Date
    Oct 2002
    Posts
    171
    Alright, will do. Thankyou for your help.
    Eat long and prosper!
    If someone helps you, find someone you can help.
    If you still have time, click on the darn banner up there and help the forum!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width