I have a string like this
我有这样的字符串
"C://Documents/TestUser/WWW/Help/Files/Move_Help.txt"
and have to replace Move_Help.txt
with Move_Job.txt
并且必须用Move_Job.txt替换Move_Help.txt
I am using the below code in VBA EXCEL
我在VBA EXCEL中使用以下代码
str = "C://Documents/TestUser/WWW/Help/Files/Move_Help.txt"
rlpStr = Replace(str, 'Help', 'Job')
I am getting
我正进入(状态
"C://Documents/TestUser/WWW/Job/Files/Move_Job.txt"
Expected
"C://Documents/TestUser/WWW/Help/Files/Move_Job.txt"
Can you please help on this.
你能帮忙吗?
FYI : I can't match Move_Help to Move_Job (Move_ is not constant. It can be any string)
仅供参考:我无法将Move_Help与Move_Job匹配(Move_不是常量。它可以是任何字符串)
2 个解决方案
#1
9
There's a one-line solution for this:
有一个单线解决方案:
rlpStr = StrReverse(Replace(StrReverse(str), StrReverse("Help"), StrReverse("Job"), , 1))
Technically, it's slightly less efficient than combining InStr
and Replace
but it can be used inside another expression if you need to. Also, I like the one-line solutions so long as they're not incomprehensible.
从技术上讲,它的效率略低于组合InStr和Replace,但如果需要,它可以在另一个表达式中使用。此外,我喜欢单线解决方案,只要它们不是不可理解的。
#2
5
Would the technique in the code below meet your requirement?
以下代码中的技术是否符合您的要求?
The intial value of Str is:
Str的初始值是:
C://Documents/TestUser/WWW/Help/Files/Move_Help.txt
The final value is:
最终价值是:
C://Documents/TestUser/WWW/Help/Files/Move_Job.txt
The code uses InStrRev
to locate the last occurrence of ValueCrnt
, if any, If ValueCrnt
is present, it replaces that final occurrence with ValueNew
.
代码使用InStrRev来定位ValueCrnt的最后一个匹配项(如果有),如果存在ValueCrnt,它将使用ValueNew替换最后一次出现。
Option Explicit
Sub Demo()
Dim Pos As Long
Dim Str As String
Dim ValueCrnt As String
Dim ValueNew As String
Str = "C://Documents/TestUser/WWW/Help/Files/Move_Help.txt"
ValueCrnt = "Help"
ValueNew = "Job"
Pos = InStrRev(Str, ValueCrnt)
If Pos > 0 Then
Str = Mid(Str, 1, Pos - 1) & Replace(Str, ValueCrnt, ValueNew, Pos)
End If
Debug.Print Str
End Sub
#1
9
There's a one-line solution for this:
有一个单线解决方案:
rlpStr = StrReverse(Replace(StrReverse(str), StrReverse("Help"), StrReverse("Job"), , 1))
Technically, it's slightly less efficient than combining InStr
and Replace
but it can be used inside another expression if you need to. Also, I like the one-line solutions so long as they're not incomprehensible.
从技术上讲,它的效率略低于组合InStr和Replace,但如果需要,它可以在另一个表达式中使用。此外,我喜欢单线解决方案,只要它们不是不可理解的。
#2
5
Would the technique in the code below meet your requirement?
以下代码中的技术是否符合您的要求?
The intial value of Str is:
Str的初始值是:
C://Documents/TestUser/WWW/Help/Files/Move_Help.txt
The final value is:
最终价值是:
C://Documents/TestUser/WWW/Help/Files/Move_Job.txt
The code uses InStrRev
to locate the last occurrence of ValueCrnt
, if any, If ValueCrnt
is present, it replaces that final occurrence with ValueNew
.
代码使用InStrRev来定位ValueCrnt的最后一个匹配项(如果有),如果存在ValueCrnt,它将使用ValueNew替换最后一次出现。
Option Explicit
Sub Demo()
Dim Pos As Long
Dim Str As String
Dim ValueCrnt As String
Dim ValueNew As String
Str = "C://Documents/TestUser/WWW/Help/Files/Move_Help.txt"
ValueCrnt = "Help"
ValueNew = "Job"
Pos = InStrRev(Str, ValueCrnt)
If Pos > 0 Then
Str = Mid(Str, 1, Pos - 1) & Replace(Str, ValueCrnt, ValueNew, Pos)
End If
Debug.Print Str
End Sub