查找字符串中查找指定字符第二次出现的位置?(急,谢谢~~~
如题:例如:
RXOTRX-123-0
RXOTRX-5-8
RXOTRX-45-11
...
等等
要查找“-”第二次出现的位置,有没有办法阿?
我看了帮助,replace ,serch , instr 等函数好像都不行,清问有没高手知道?
谢谢~~~ --------------------编程问答--------------------
dim str1 as string--------------------编程问答--------------------
str1="RXOTRX-123-0"
dim pos as integer
pos=instr(instr(str1,"-")+1,str1,"-")
debug.print "The position of second '-':",pos
Option Explicit--------------------编程问答--------------------
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 Sub
Private Sub Form_Load()
SearchString = "RXOTRX-123-0-123"
End Sub
--------------------编程问答-------------------- 不知道有谁研究过,Replace和instr哪个效率高? --------------------编程问答-------------------- 比不出:
'replace,instr都可以
Dim s As String
s = "RXOTRX-45-11"
Debug.Print InStr(Replace(s, "-", "A", , 1), "-")
Option Explicit--------------------编程问答-------------------- 不知道有谁研究过,Replace和instr哪个效率高?
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 Sub
Private Sub Command2_Click()
Print Timer
Dim s As String
s = "RXOTRX-45-11"
Debug.Print InStr(Replace(s, "-", "A", , 1), "-")
Print Timer
End Sub
Private Sub Form_Load()
SearchString = "RXOTRX-123-0"
End Sub
-------------------------------------------
这个要看具体情况吧?replace和instr都是VB中速度较快的函数,在LZ这样的要求下没什么可比性
我想如果在不同的情况下应该是各有优势吧,比如必一下LZ的要求,一个字符串中有100个"-",要找第77个"-"的位置,那么只用instr效率肯定不如replace+instr吧? --------------------编程问答-------------------- 一次咋能比得出来,循环它一万次试试 --------------------编程问答-------------------- LZ这个这个直接比一万次可能是instr快 --------------------编程问答-------------------- LZ这个确实是instr快,而且快挺多的
循环十万次,replace在27到28左右,instr在4左右
可能是因为replace要走遍整个字符串,还有额外的替换操作 --------------------编程问答-------------------- N,听说过instr是VB最快的函数之一 --------------------编程问答-------------------- replace + inter
恩! 以前怎么没想到这2个配合....
CSDN上学习确实不错!!
以前解决这个问题的方法是
>>记住第一个的位置 放变量a
>>干掉前面字符
>>在次用inter获得字符的位置 放变量b
a+b = 第二个字符的位置!
这样代码量就多了...
补充:VB , 数据库(包含打印,安装,报表)