Author Topic: Replacing a caracter in a VBA string  (Read 3039 times)

0 Members and 1 Guest are viewing this topic.

iliekater

  • Guest
Replacing a caracter in a VBA string
« on: August 27, 2007, 02:19:32 PM »
I wonder if it is possible to replace a caracter in a string . For example in the following string :
Code: [Select]
"d:\Program files\Folder 1\Sub folder 2"i'd like to replace the caracter "\" with "\\" , resulting :
Code: [Select]
"d:\\Program files\\Folder 1\\Sub folder 2"Is it possible ?

I want to do this in order to convert some paths determined by VBA , to paths that Lisp will understand (it uses \\ instead of \) .
« Last Edit: August 27, 2007, 02:21:29 PM by iliekater »

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Replacing a caracter in a VBA string
« Reply #1 on: August 27, 2007, 02:25:25 PM »
yes, use Instr() to find it, add it at the positions found
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Replacing a caracter in a VBA string
« Reply #2 on: August 27, 2007, 02:35:43 PM »
I use REPLACE

Code: [Select]
Replace(expression, find, replace[, start[, count[, compare]]])

Code: [Select]
Public Sub Main()
    Dim strNewString As String
   
    strNewString = Replace("d:\\Program files\\Folder 1\\Sub folder 2", "\\", "\", 1)
    MsgBox strNewString
End Sub
« Last Edit: August 27, 2007, 02:37:09 PM by Matt W »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Replacing a caracter in a VBA string
« Reply #3 on: August 27, 2007, 02:41:18 PM »
Use replace

Code: [Select]
Dim Path as String
Dim NewPath As String

Path = "d:\Program files\Folder 1\Sub folder 2"

NewPath = Replace (Path, "\", "\\")

NewPath will equal "d:\\Program files\\Folder 1\\Sub folder 2"


dangit .. Matt beat me to the punch ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Guest

  • Guest
Re: Replacing a caracter in a VBA string
« Reply #4 on: August 27, 2007, 02:42:01 PM »

dangit .. Matt beat me to the punch ...


Woohoo!!

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Replacing a caracter in a VBA string
« Reply #5 on: August 27, 2007, 02:42:45 PM »

dangit .. Matt beat me to the punch ...



Woohoo!!

I need to learn to type faster ...

<edit* and I obviously need to pay closer attention to what I am doing>
« Last Edit: August 27, 2007, 02:55:30 PM by Keith™ »
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Replacing a caracter in a VBA string
« Reply #6 on: August 27, 2007, 03:01:26 PM »
I knew there was an easier way, I just couldn't remember REPLACE off the top of my head
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Maverick®

  • Seagull
  • Posts: 14778
Re: Replacing a caracter in a VBA string
« Reply #7 on: August 27, 2007, 05:50:29 PM »

dangit .. Matt beat me to the punch ...



Woohoo!!

I need to learn to type faster ...

<edit* and I obviously need to pay closer attention to what I am doing>

Naw, He has just been waxing on and waxing off more than you.  :-D

iliekater

  • Guest
Re: Replacing a caracter in a VBA string
« Reply #8 on: August 27, 2007, 06:27:46 PM »
Thanks alot guys . It's going to be helpful . :-)

Dnereb

  • Guest
Re: Replacing a caracter in a VBA string
« Reply #9 on: August 30, 2007, 10:22:33 AM »
What about C:\test\\\Stuff.dwg

Public Function RemoveRepetitiveBackSlashes( StrText, Optional AlternativeString as String) as String

Dim StrFind as String
Dim StrDoulblure as String

if Len (Alternative) > 0 then
    StrFind = AlternativeString
else
    StrFind = "\"
end if

StrDoublure = StrFind & StrFind

Do While( Len(RemoveRepetitiveSlashes ) <> Len(StrText)
    RemoveRepetitiveSlashes = StrText
    StrText = Replace(StrText, "\", "\\")
loop

End Function