我正在尝试在经典的ASP应用程序中拆分字符串,在页面中包含以下代码,但它似乎不起作用。还有另一个问题看起来很相似,但是处理的问题类型不同,我已经去过那里了,对他们没有帮助。任何帮助,将不胜感激。

<%
Dim SelectedCountries,CitizenshipCountry, Count
SelectedCountries = "IN, CH, US"
CitizenshipCountry = Split(SelectedCountries,", ")
Count = UBound(CitizenshipCountry) + 1
Response.Write(CitizenshipCountry[0])
Response.End
%>

最佳答案

您犯了两个错误,这就是为什么您没有获得预期的结果。


检查数组的边界时,您需要指定Array变量,在这种情况下,该变量是由Split()生成的变量,即CitizenshipCountry
通过在括号((...))中而不是方括号([...])中指定元素顺序位置来访问数组元素。


试试这个:

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


我想做的是在调用IsArray以避免这些类型的错误之前,使用UBound()检查变量是否包含有效数组。

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

09-30 16:52