Sunday, 15 July 2012

Replace only last occurrence of match in a string in VBA -



Replace only last occurrence of match in a string in VBA -

i have string

"c://documents/testuser/www/help/files/move_help.txt"

and have replace move_help.txt move_job.txt

i using below code in vba excel

str = "c://documents/testuser/www/help/files/move_help.txt" rlpstr = replace(str, 'help', 'job')

i getting

"c://documents/testuser/www/job/files/move_job.txt"

expected

"c://documents/testuser/www/help/files/move_job.txt"

can please help on this.

fyi : can't match move_help move_job (move_ not constant. can string)

there's one-line solution this:

rlpstr = strreverse(replace(strreverse(str), strreverse("help"), strreverse("job"), , 1))

technically, it's less efficient combining instr , replace can used within look if need to. also, one-line solutions long they're not incomprehensible.

string vba excel-vba replace

No comments:

Post a Comment