|
本帖最后由 aoe1981 于 2019-11-16 09:49 编辑
同70楼,原来我更辛苦。
最近注意到了竞赛题:
[第127期]替换特定规则范围内的字符(进行中……)
http://club.excelhome.net/thread-1506418-1-1.html
(出处: ExcelHome技术论坛)
想必源头是这儿。
一时间有点蠢蠢欲动,跃跃欲试。无奈对于神器“正则表达式”以前似乎通了一窍,如今确定是全都堵了,便难受地只是用寻常的法子做了一下,不想,“眼前有景道不得,崔颢题诗在上头”。
好在我脸皮厚,还是贴了出来。
- Private Sub TH()
- Dim i&, n&, S$, cr$(), pd1() As Boolean, pd2() As Boolean
- S = "qwe100eh1eee2e345exxe678ee999eexc"
- n = Len(S)
- ReDim cr$(1 To n), pd1(1 To n) As Boolean, pd2(1 To n) As Boolean
- For i = 1 To n
- cr(i) = Mid(S, i, 1)
- Next i
- For i = 2 To n
- If cr(i - 1) Like "#" And cr(i) = "e" Then pd1(i) = True
- If pd1(i - 1) And cr(i) = "e" Then pd1(i) = True
- Next i
- For i = n - 1 To 1 Step -1
- If cr(i + 1) Like "#" And cr(i) = "e" Then pd2(i) = True
- If pd2(i + 1) And cr(i) = "e" Then pd2(i) = True
- Next i
- For i = 2 To n - 1
- If pd1(i) And pd2(i) Then cr(1) = cr(1) & "2" Else cr(1) = cr(1) & cr(i)
- Next i
- MsgBox S & Chr(10) & cr(1) & cr(n)
- End Sub
复制代码
其实,实在的原因是,我从兄台的代码中温习了一招:mid函数的巧用。这一招印象中香川曾讲过,但我不用,束之高阁,早都忘了。今日算是领教了。我又写了一段测试代码:
- Public Sub test1()
- Dim S$
- S = "qwe1"
- Debug.Print S, (Mid(S, 3, 1) = "e")
- Debug.Print S, (Mid(S, 3, 1) = "#")
- Mid(S, 3, 1) = "#"
- Debug.Print S, (Mid(S, 3, 1) = "e")
- Debug.Print S, (Mid(S, 3, 1) = "#")
- End Sub
复制代码
学如逆水行舟,不进则退、退、退……啊
|
评分
-
1
查看全部评分
-
|