开发者

splitting a string in VB

I have a ComboBox which I assign to a variable:

Dim var as String = ComboBox1.SelectedValue 
Dim name As String = var.Split(",")

This gives me the error

Value of type '1-dimensional array of string' cannot be converted to String

Any ideas as to where I'm going 开发者_运维知识库wrong?


Split returns an array of strings. Your variable needs to be changed to an array, not just a single string.


My VB is a bit rusty, but I think you have to make name an array:

Dim name() As String = var.Split(",")


name needs to be declared as an array.

dim name() as string = var.split(",")

The split() method will break up the string based on the given character and put each newly created string into an array and return it.

This is what your error message is telling you:

Value of type '1-dimensional array of string' cannot be converted to String

The method returns an array of string, but your trying to put it into just a string!

EDIT: In response to your answer...

So far you've managed to split the string yourself with the split method. To output this to your message box, you need to concatenate the two elements in the proper order:

msgbox(name(1) & " " & name(0))

Notice I indexed the array twice! Element 1 is the first name, element 0 is the last name. Remember you got this name in lname,fname format. Passing the array itself doesn't make sense! Remember, a datatype is not equal to an array of that type, they are two different things. Therefore a string is not compatible with a string array. However, each individual element of the array is a string, and so each of those are compatible with the string type (because they're the same thing)!


 Dim var As String = ComboBox1.SelectedValue
 Dim temp() As String = Split(var, ",", -1, CompareMethod.Binary)
 Dim name As String = temp(0)


Or maybe "name" isn't an array and the goal is to populate "name" with everything up until the first comma, in which case the fix would be:

Dim name as String = var.Split(",")(0)

Note: assumes that var is not Nothing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜