gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:16:20 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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

    关于arrays - 如何在经典asp中拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42092929/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com