如题:
例如:
RXOTRX-123-0
RXOTRX-5-8
RXOTRX-45-11
...
等等 要查找“-”第二次出现的位置,有没有办法阿?
我看了帮助,replace ,serch , instr 等函数好像都不行,清问有没高手知道? 谢谢~~~
例如:
RXOTRX-123-0
RXOTRX-5-8
RXOTRX-45-11
...
等等 要查找“-”第二次出现的位置,有没有办法阿?
我看了帮助,replace ,serch , instr 等函数好像都不行,清问有没高手知道? 谢谢~~~
str1="RXOTRX-123-0"
dim pos as integer
pos=instr(instr(str1,"-")+1,str1,"-")
debug.print "The position of second '-':",pos
Dim SearchString As String
Dim SearchChar As String
Dim MyPos() As Integer
Dim i As Integer
Private Sub Command1_Click()
ReDim MyPos(1 To Len(SearchString))
SearchChar = "-" ' 要查找字符串 "-"
For i = 1 To Len(SearchString) - 1
MyPos(i) = InStr(i, SearchString, SearchChar, 1)
Next
For i = 1 To Len(SearchString) - 1
If MyPos(i) <> MyPos(i + 1) Then
Print MyPos(i)
End If
Next
End SubPrivate Sub Form_Load()
SearchString = "RXOTRX-123-0-123"
End Sub
s = "RXOTRX-45-11"
Debug.Print InStr(Replace(s, "-", "A", , 1), "-")
Option Explicit
Dim SearchString As String
Dim SearchChar As String
Dim MyPos() As Integer
Dim i As Integer
Private Sub Command1_Click()
Print Timer
ReDim MyPos(1 To Len(SearchString))
SearchChar = "-" ' 要查找字符串 "-"
For i = 1 To Len(SearchString) - 1
MyPos(i) = InStr(i, SearchString, SearchChar, 1)
Next
For i = 1 To Len(SearchString) - 1
If MyPos(i) <> MyPos(i + 1) Then
Print MyPos(i)
End If
Next
Print Timer
End SubPrivate Sub Command2_Click()
Print Timer
Dim s As String
s = "RXOTRX-45-11"
Debug.Print InStr(Replace(s, "-", "A", , 1), "-")
Print Timer
End SubPrivate Sub Form_Load()
SearchString = "RXOTRX-123-0"
End Sub
-------------------------------------------
这个要看具体情况吧?replace和instr都是VB中速度较快的函数,在LZ这样的要求下没什么可比性
我想如果在不同的情况下应该是各有优势吧,比如必一下LZ的要求,一个字符串中有100个"-",要找第77个"-"的位置,那么只用instr效率肯定不如replace+instr吧?
循环十万次,replace在27到28左右,instr在4左右
可能是因为replace要走遍整个字符串,还有额外的替换操作
>>记住第一个的位置 放变量a
>>干掉前面字符
>>在次用inter获得字符的位置 放变量b
a+b = 第二个字符的位置!这样代码量就多了...