數字字符串的匹配問題[注:只對有效的方法給分]

bozy 2004-12-03 06:02:26
有個數字字符串,以 ,分割。
比方:
str="9,12,13,168,234"

如何判斷 數字 char 是否存在于str 中。

要求:
比方,char=1 時,返回false;
char=12 時,返回true


我知道可以將str轉化為數組再進行匹配,
有沒有其他更好的辦法呢?
請帖出代碼。

...全文
110 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
bozy 2004-12-06
  • 打赏
  • 举报
回复
to: meizz(梅花雪)
久仰大名,果真達人.代碼的實現正如我意!
用數組的方式實現,我早想過,但縂感覺有方法執行效率會更好。
看來我對正則確實太過欠缺,今後得好好補一補.
我自己再把它改爲函數就可以解決我的實際問題了.

另:請各位原諒,大家給我出謀劃策,很感謝大家。
把全分給能完整解決問題的人,我覺得是對勞動的最好尊重。
再次感謝大家!!
meizz 2004-12-06
  • 打赏
  • 举报
回复
这种问题有正则当然是最好的解决之道:
<SCRIPT LANGUAGE="VBScript">
Dim re, str, str2
str="9,12,13,168,234"
str2 = "1"

set re=new RegExp
re.global=true
re.pattern="(^|,)"& str2 &"(,|$)"
msgbox re.Test(str)
</SCRIPT>
bozy 2004-12-06
  • 打赏
  • 举报
回复
to: yayaleon801030(yayaleon)
------------------
temp1 = ","& replace(strAll, " ", "") &","
temp2 = ","& replace(strNum, " ", "") &","
-----------------
不太明白replace 空格的意思

@_@
yayaleon801030 2004-12-06
  • 打赏
  • 举报
回复
function existNum(strAll, strNum)
temp1 = ","& replace(strAll, " ", "") &","
temp2 = ","& replace(strNum, " ", "") &","
if ( instr(temp1, temp2)>0 ) then
existNum = true
else
existNum = false
end if
end function
comszsoft 2004-12-06
  • 打赏
  • 举报
回复
777dragon(风林火山) ( )
同意
bozy 2004-12-06
  • 打赏
  • 举报
回复
頂一下,看看有沒有更好的辦法。
bozy 2004-12-05
  • 打赏
  • 举报
回复
很谢谢大家的意见,
如果能用字符串的方式解决,那就更perfect了。
尚和空四 2004-12-03
  • 打赏
  • 举报
回复
支持 777dragon(风林火山)
davs 2004-12-03
  • 打赏
  • 举报
回复
用Split可以解决问题,楼上正解啦
777dragon 2004-12-03
  • 打赏
  • 举报
回复
呵呵~还是觉得数组好用 ^_^`
777dragon 2004-12-03
  • 打赏
  • 举报
回复
~
function ChkNum(arrTest,key)
arrSplit = Split(arrTest, ",")
for i=0 to ubound(arrSplit)
if arrSplit(i)=key then
ChkNum=true
exit function
end if
next
ChkNum=false
end function

arrtest="9,12,13,168,234"
response.write ChkNum(arrTest,"1")
response.write ChkNum(arrTest,"12")
response.write ChkNum(arrTest,"234")
ygjwjj 2004-12-03
  • 打赏
  • 举报
回复
function result(str,snum)
str=","&str & ","
snum=","&snum &","
if instr(str,snum)>0 then
result=true
else
result=false
end if
end function
str="9,12,13,168,234"
response.write result(str,9)
response.write result(str,234)
bozy 2004-12-03
  • 打赏
  • 举报
回复
to: lienzhu(李强) ,你的這個思路很有創意。
這個方法還有一個小的問題,
如果是 snum=234 ,那就會顯示錯誤的結果了。

str只能是如上的形式 "9,12,13,168,234"
ygjwjj 2004-12-03
  • 打赏
  • 举报
回复
response.write result(str,34)

true?????
lienzhu 2004-12-03
  • 打赏
  • 举报
回复
function result(str,snum)
str=str & ","
snum=snum &","
if instr(str,snum)>0 then
result=true
else
result=false
end if
end function
str="9,12,13,168,234"
response.write result(str,1)
response.write result(str,12)

28,391

社区成员

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

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