请问如何获取字符串在数组中的位置

Yookey 2009-04-30 10:25:56
比如现在有一个数组a
a="abc|ab|efg|hi"

有个字符串b
b="ab"

数组操作
a=split(a,"|")


请教各位前辈 能否编写个ASP自定义函数 获取字符串b在数组a中的位置?

另外需要注意的是 在上面的例子中我们可以看出来 a(1)="ab", 但a(0)中也包含"ab", 如何避免这样的bug?

望各位前辈不吝赐教,小弟感激不尽
...全文
152 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yookey 2009-04-30
  • 打赏
  • 举报
回复
感谢,shenzhenNBA 可惜不能给你分了抱歉
shenzhenNBA 2009-04-30
  • 打赏
  • 举报
回复
那么快结帖了,呵呵
shenzhenNBA 2009-04-30
  • 打赏
  • 举报
回复
功能函数
-------------------------------------------------------
function getposition(xstr,keyword)
dim sw, k, ar
dim i
i=0
sw=xstr
k=keyword
ar=split(sw,"|")
do while i<ar.ubound(ar)-1
if ar[i]=k then
getposition=i
exit function
end if
i=i+1
loop
end function
-------------------------------------------------------
a="abc|ab|efg|hi"
有个字符串b ,b="ab"
数组操作 a=split(a,"|")
获取字符串b在数组a中的位置

应用:
dim posi
posi=getposition(a,b)

Yookey 2009-04-30
  • 打赏
  • 举报
回复
非常感谢 2位!
街头小贩 2009-04-30
  • 打赏
  • 举报
回复
再给你一个:

'查询是否存在该值
Function seekArrayVal(strArr,strVal)
Dim strBool:strBool=false
If Not IsArray(strArr) Or strVal="" then seekArrayVal=strBool
For Each tmp in strArr
If strVal=tmp then
strBool=true
Exit For
End If
Next
seekArrayVal=strBool
End Function

街头小贩 2009-04-30
  • 打赏
  • 举报
回复

<%
'返回值在数组中的下标
Function getArrayIndex(strArr,strVal)
Dim strIndex,J
If NOT IsArray(strArr) Or strVal="" then strIndex=-1
For J=0 to UBound(strArr)
If IsNumeric(strVal) then
If Int(Abs(strVal))-strArr(J)=0 then strIndex=J
ElseIf IsObject(strVal) Or IsDate(strVal) Or IsArray(strVal) then
strIndex=-1
Else
If Trim(strVal)=strArr(J) then strIndex=J
End If
Next
getArrayIndex=strIndex
End Function
Dim a:a="abc|ab|efg|hi"
Dim b:b="ab"
Response.Write Split(a,"|")(getArrayIndex(Split(a,"|"),b)) &"<br/>"
Response.Write getArrayIndex(Split(a,"|"),b)
%>


lzp4881 2009-04-30
  • 打赏
  • 举报
回复
<%
a="abc|ab|efg|hi"
b="ab"
response.write "b在a中的位置是"&getN(a,b)
%>
<%
function getN(x,y)
getN=0
arr=split(x,"|")
for i=0 to ubound(arr)
if arr(i)=y then
getN=i+1
exit function
end if
next
end function
%>

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧