如何在经典ASP中拆分字符串 [英] How to split a string in classic asp
问题描述
我正在尝试在经典的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
%>
推荐答案
您犯了两个错误,这就是为什么您未获得预期结果的原因.
You've made a couple of mistakes which is why you are not getting the expected result.
-
在检查数组的边界时,您需要指定Array变量,在这种情况下,该变量是
Split()
生成的变量,它是CitizenshipCountry
.
通过在括号((...)
)中指定元素顺序位置而不是方括号( [...]
).
Array elements are accessed by specifying the element ordinal position in parentheses ((...)
) not square brackets ([...]
).
尝试一下:
<%
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()
%>
我想做的是在调用 UBound()
之前使用 IsArray
检查变量是否包含有效数组,以避免这些类型的错误.
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()
%>
这篇关于如何在经典ASP中拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!