|
-
Feb 6th, 2003, 11:58 AM
#1
Thread Starter
Addicted Member
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! 
-
Feb 6th, 2003, 12:08 PM
#2
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))
-
Feb 6th, 2003, 01:51 PM
#3
Frenzied Member
myString.Split("") doesn't split the text on each character when i try. Upper bound of the array is 0.
~Peter

-
Feb 6th, 2003, 02:18 PM
#4
Thread Starter
Addicted Member
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! 
-
Feb 6th, 2003, 02:42 PM
#5
Member
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
-
Feb 6th, 2003, 02:51 PM
#6
Actually a string is just an array of char and you can access that array with the Char property of the string variable.
-
Feb 6th, 2003, 06:04 PM
#7
Fanatic Member
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.
yes what he said, Fat_N_Furry is it really necesary to convert it into an array?
-
Feb 6th, 2003, 08:39 PM
#8
Thread Starter
Addicted Member
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! 
-
Feb 7th, 2003, 01:23 AM
#9
Member
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.
-
Feb 7th, 2003, 11:00 AM
#10
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|