asp 字符串截取问题

来源:百度知道 编辑:UC知道 时间:2024/05/08 12:05:57
比如一个字符串

ss = "1,2,3,4,5,6,7,8"

我想截取 第 N 个逗号以前的字符,应该怎么写呢? 谢谢

或者结合 Split 和 Join 两个函数来实现你想要的功能:

Private Function GetString(ByVal sSource, ByVal n)
Dim sItem
'// 分隔指定的字符串给一个数组
sItem = Split(sSource, ",", n + 1, 1)
'// 重定义数组,以去掉后面的部份
ReDim Preserve sItem(n - 1)
'// 重新组织数组,以还原成一个字符串
GetString = Join(sItem, ",")
End Function

Response.Write GetString("1,2,3,4,5,6,7,8", 3)

function getIndex(ss,s,pos,next)
dim i
i = instr(pos,ss,s)
if next>1 then
getIndex=getIndex(ss,s,i,next-1)
else
getIndex=i
end if
end function
dim ss,stmp
ss="1,2,3,4,5,6,7,8"
stmp = left(ss,getIndex(ss,",",1,7))

sub substr()
pos=instr(pos,str,",")
num=num+1
if num<n and pos<len(str) then substr
end sub

dim pos : pos=1
dim num : num=0

dim N : N=3
dim ss : ss="1,2,3,4,5,6,7,8"

subs