plz help me about vb 6.0

2007-03-14 4:52 am
how to program this:

Q.1
if entry 123**----------------------------------> the result is 123
if entry 444**13646546---------------------->the result is 444


Q. 2
if entry **123**-------------------------------------------------->the result is 123
if entry 123165468**111**5646489986-----------------> the result is 111


can u teach me? thx so much!!!

回答 (1)

2007-03-14 10:57 pm
✔ 最佳答案
Please try following function:

Function GetToken(ByVal sVal As String, sDel As String, nPos As Integer) As String
Dim nPos1, nSet As Integer
Dim i As Integer
nPos1 = 1
nSet = 0
For i = 1 To Len(sVal) - Len(sDel) + 1
If Mid(sVal, i, Len(sDel)) = sDel Then
nSet = nSet + 1
If nSet = nPos Then
GetToken = Mid(sVal, nPos1 + IIf(nSet = 1, 0, Len(sDel)), i - nPos1 - IIf(nSet = 1, 0, Len(sDel)))
Exit Function
End If
nPos1 = i
End If
Next
If nPos > 1 And nSet + 1 = nPos Then
GetToken = Mid(sVal, nPos1 + Len(sDel), Len(sVal) - nPos1 - Len(sDel) + 1)
Else
GetToken = ""
End If
End Function

And check with following code:

Sub ChkGetTok()
MsgBox GetToken("123**456**789**000", "**", 1)
MsgBox GetToken("123**456**789**000", "**", 2)
MsgBox GetToken("123**456**789**000", "**", 3)
MsgBox GetToken("123**456**789**000", "**", 4)
MsgBox GetToken("123**", "**", 1)
MsgBox GetToken("**123**", "**", 2)
MsgBox GetToken("444**13646546", "**", 1)
MsgBox GetToken("123165468**111**5646489986", "**", 2)
End Sub


收錄日期: 2021-04-21 13:28:04
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070313000051KK03981

檢視 Wayback Machine 備份