VB中截取任意两个字符中间的字符串函数

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

vb截取字符串2010-04-18 02:52一、

(1)左部截取left$(字符串,n):n是要截取的字符个数
(2)中部截取Mid$(字符串,p,n):表示从第p个字符开始截取n个字符
(3)右部截取Right$(字符串,n):表示截取字符串的后n个字符
如:
left$("abcdefg",3)的结果是:"abc"
mid$("abcdefg",2,3)的结果是:"bcd"
right$("abcdefg",3)的结果是:"efg"

Dim strSrc As String
Dim strResult As String

strSrc = "0|下载地址1|/upload/upload2/files/1.text"
strResult = Right(strSrc, Len(strSrc) - InStr(1, strSrc, "/"))

二、

VB中截取任意两个字符中间的字符串函数
111111+22222+33333333+44444+55555+66666…

如何得到任意两个符号”+”之间的字符串,譬如22222

以下为代码全过程,函数为增强版,支持任意字符,只需要在调用的时候修改参数就OK了,代码注释中已经有描述:
新建工程在窗体上放置两个TextBox控件和一个Command控件,名称不需要修改默认就OK,分别名为:text1.text text2.text 和 command1
然后复制以下代码:

Private Sub Command1_Click()
Text2.Text = GetChar(Trim(Text1.Text), "+", 1, 3)
End Sub

'函数使用方法: getchar(mStr:原始字符,charX:规律字符,比如截取两段加号之间的那么就输入"+",
'BofNum: 起始字符位, EofNum: 终点字符位)
Private Function GetChar(mStr As String, charX As String, BofNum As Integer, EofNum As Integer) As String
Dim i As Integer '循环
Dim x As Integer '记录第几个"+"符号
Dim startNum As Integer 'MID函数截取开始位数
Dim endNum As Integer 'MID函数截取结束位数
For i = 1 To Len(mStr)
If x = EofNum Then Exit For '得到截取开始和结束位数则退出
If Mid(mStr, i, 1) = charX Then
x = x + 1
If x = BofNum Then '第一个为开始位置
startNum = i
ElseIf x = EofNum Then '第二为结束位置
endNum = i
End If
End If
Next i

'qnames2 = finddoc.MTnames
Dim sArray() As String
'Dim i As Integer '循环
Dim iCount As Integer '记录第几个"+"符号
iCount = 1
'For i = 1 To Len(qnames)
' sArray(i-1)= Mid(qnames, iCount, 1)
' iCount =iCount + 2
' Msgbox sArray(i-1)
'Next

'开始位置第一个是符号 需要移动一位 结束位置-开始位置=截取长度
'截取出来的两边各有符号 所以需要取舍
GetChar = Mid(mStr, startNum + 1, endNum - startNum - 1) '赋值
End Function
三、mid()函数法
aa="abcdA123456Bdff"

asite = instr(aa,"A")

bsite = instrRev(aa,"B")

Response.Write(mid(aa,asite+1,bsite-asite-1))

--------------------

---------------------
function cutstr(byval str1 as string,byval StartStr as string,byval EndStr as string) as
string
dim i as integer,j as integer,L1 as integer,L2 as integer
l1=len(startstr)
l2=len(endstr)
i=instr(str,startStr)
j=instr(str,endStr)
if (l1*l2=0) or (i*j=0) or (j-i<1) then '如果指定起始字符串与结束字符串有一个为空,或者有一个不存在,或者前后位置不正确,返回源字符串
cutstr=str1
exit function
else
cutstr=mid(str1,i+l1,j-l1-i)
end if
end function

dim str as string ,s1 as string ,s2 as string,s3 as string
str="[name]ASDADASDSA[year]123456789[number]123456"
s1="[name]"
s2="[year]"
s3="[number]"
msgbox cutstr(str,s1,s2)
msgbox cutstr(str,s2,s3)
---------------------------------------
dim str
wstr=[name]ASDADASDSA[year]123456789[number]123456
str=mid(wstr,instr(wstr,"[name]"),InStrRev(wstr, "[year]") - InStr(wstr, "[name]"))
str1=mid(wstr,instr(wstr,"[year]"),InStrRev(wstr, "[number]") - InStr(wstr, "[year]"))
-------------------------------------


Private Sub Form_Click()
s = "[name]ASDADASDSA[year]123456789[number]123456"
a = Split(s, "]")
For i = 1 To UBound(a)
Print Split(a(i), "[")(0)
Next
End Sub

-----------



相关文档
最新文档