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

How to split a string in classic asp

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

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

你犯了几个错误,这就是你没有得到预期结果的原因。

  1. 当检查数组的边界时,您需要指定数组变量,在这种情况下,由 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()
%>

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

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