如何在经典的asp中拆分字符串

Vip*_*bey 4 arrays string vbscript split asp-classic

我试图在经典的asp应用程序中拆分一个字符串,在页面中有下面的代码,它似乎不起作用.还有一个问题看起来很相似但是处理不同类型的问题,我已经找到了那里的答案而且他们没有帮助.任何帮助,将不胜感激.

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

Lan*_*art 5

你犯了几个错误,这就是为什么你没有得到预期的结果.

  1. 在检查Array的边界时,您需要指定Array变量,在这种情况下,生成的变量Split()CitizenshipCountry.

  2. 通过在括号((...))中指定元素序号位置而不是方括号([...])来访问数组元素.

试试这个:

<% 
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()
%>
Run Code Online (Sandbox Code Playgroud)

我喜欢做的是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()
%>
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,这就像一个魅力,我是classis asp的新手,从来没有使用过,只需要在旧应用上做一些修复,谢谢你的帮助:)(Y). (3认同)