Split CSV into String array

So I have a dictionary<String, Object> That I’m assigning to my variables, coming from the Queue.
One of them is for the service type, and there may be one service listed, or there may be more than one service listed. So coming in from the Queue I might have:

{Internet}
OR
{Internet, Phone, Prism}

I’m trying to assign these to a string array, and I’m trying to split it up right there when It’s being taken from the Queue. So this is what I did:

Data(“service”).ToString.Split(“,”)

The error I’m getting:

Option Strict on disallows implicit conversions from ‘String’ to ‘Char’

I thought that .split() returns an array of strings… Which is what I’m assigning it to.
What am I doing wrong here… How do I get this done most efficiently.

Please, check your Split expression. Function String.Split() doesn’t have overload that have one string parameter. You can fix your expression Data(“service”).ToString.Split(","c). Where last character “c” is indication that this constant literal not String but Char. Or choose something with string directly but usually it have couple more parameter.

1 Like

I changed it a little. I am now just assigning the object from the dictionary to a string variable.
So now I’ve got my string. I’m trying to split it up, and what you are saying is that I have to cast it as a char.
I tried it with multiple parameters and it wasn’t working. I’m about to try it a little differently now. I’ll let you know how it goes.

I thought I’d give a few tips on this.
To split by a character, you need to use “,“c or “,”(0), but this doesn’t allow for any options.
To split by a string, you need to use a character array, like {”,”}, StringSplitOptions
So it would look like this:
Data("service").ToString.Split(","c)
or
Data("service").ToString.Split({","},System.StringSplitOptions.None)

However, Split() will not work if the character is not contained in the string, so you need an additional condition. Here I will normally use inline If condition, like this:
If(Data("service").ToString.Contains(","), Data("service").ToString.Split(","c), {Data("service").ToString})
So that will make it an array of either multiple items or one item.

Now, I’m not saying this is the best approach to what you are trying to do though. And, there are other ways to convert a string to an array (like JArray conversion for example). But, if you choose to take the approach of splitting a string by a character, then that’s how you would do it.

EDIT: I was mistaken, you don’t need to check if “,” is contained in the string.

Regards.

2 Likes

Hi @ClaytonM. I think you are doing something wrong.
If string x doesn’t contains “,” than result x.Split(","c) would be one-element array of {x}.

If(Data("service").ToString.Contains(","), Data("service").ToString.Split(","c), {Data("service").ToString})

It means you can just leave
Data("service").ToString.Split(","c)

2 Likes

Yes, you are right.
It must of was a bug in an old version back in 2016 or 2017, or I’m thinking of something different.

Thanks.

1 Like

Your approach make sense if you try to use for example second element.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.