Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string in classic asp

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
%>
like image 769
Vipin Dubey Avatar asked Mar 10 '23 22:03

Vipin Dubey


1 Answers

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()
%>
like image 187
user692942 Avatar answered Mar 13 '23 00:03

user692942