I am trying to split a string in a classic asp application, have below code in the page and it does not seem to work. There is another question which looks similar but deals with different type of problem, I have already been to the answers there and they don't help. Any help would be appreciated.
<%
Dim SelectedCountries,CitizenshipCountry, Count
SelectedCountries = "IN, CH, US"
CitizenshipCountry = Split(SelectedCountries,", ")
Count = UBound(CitizenshipCountry) + 1
Response.Write(CitizenshipCountry[0])
Response.End
%>
You've made a couple of mistakes which is why you are not getting the expected result.
When checking the bounds of an Array you need to specify the Array variable, in this case, the variable generated by Split()
which is CitizenshipCountry
.
Array elements are accessed by specifying the element ordinal position in parentheses ((...)
) not square brackets ([...]
).
Try this:
<%
Dim SelectedCountries, CitizenshipCountry, Count
SelectedCountries = "IN, CH, US"
CitizenshipCountry = Split(SelectedCountries,", ")
'Get the count of the array not the string.
Count = UBound(CitizenshipCountry)
'Use (..) when referencing array elements.
Call Response.Write(CitizenshipCountry(0))
Call Response.End()
%>
What I like to do is use IsArray
to check the variable contains a valid array before calling UBound()
to avoid these types of errors.
<%
Dim SelectedCountries, CitizenshipCountry, Count
SelectedCountries = "IN, CH, US"
CitizenshipCountry = Split(SelectedCountries,", ")
'Get the count of the array not the string.
If IsArray(CitizenshipCountry) Then
Count = UBound(CitizenshipCountry)
'Use (..) when referencing array elements.
Call Response.Write(CitizenshipCountry(0))
Else
Call Response.Write("Not an Array")
End If
Call Response.End()
%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With