How to split a string in classic asp

25,925

You've made a couple of mistakes which is why you are not getting the expected result.

  1. 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.

  2. 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()
%>
Share:
25,925
Vipin Dubey
Author by

Vipin Dubey

A web developer.

Updated on November 25, 2020

Comments

  • Vipin Dubey
    Vipin Dubey over 3 years

    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
    %>