Skip to Content
0
Former Member
Sep 09, 2011 at 09:29 PM

Split a string and return individual elements to an array

1771 Views

Hi all, I have a string field that varies in length that I need to split at each comma and then return each element to an array so I can convert each element to a new string based on its value. I have tried multiple loop versions that I have found both in the forums and in the help text in Crystal Reports, but I get a variety of errors. Here are a couple of the formulas I have tried:

dim a() as String
dim i
For i = 1 to Len({stringfield}) step 1
    a = split({stringfield},",")
    formula = a(i)      
Next i

The above formula highlights the a(i) in line 5 and says, "Subscript must be between 1 and the size of the array"

In the below attempt I actually including some code to try and do the conversion I mention above:

formula = ""
dim i as number
dim n as number
i = 1
n = len({stringfield})
do while i <= n
    select case split({stringfield},",")
        case "B"
            formula = "Bracelet"
        case "C"
            formula = "Clocks"
    end select
    i = i + 1
loop

The above formula highlights the split(,",") in line 7 and says, "An array must be subscripted". I also tried using If/Then statements in place of the Select Case statements in the above formula, but it would not let me go beyond one if statement with out wanting the "Loop" command following it.

Am I even on the right track with these?

We use Crystal XI, and I am using Basic syntax in the above examples. I appreciate any assistance offered.