TheSwamp

Code Red => VB(A) => Topic started by: iliekater on August 27, 2007, 02:19:32 PM

Title: Replacing a caracter in a VBA string
Post by: iliekater 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 \) .
Title: Re: Replacing a caracter in a VBA string
Post by: David Hall on August 27, 2007, 02:25:25 PM
yes, use Instr() to find it, add it at the positions found
Title: Re: Replacing a caracter in a VBA string
Post by: Guest 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
Title: Re: Replacing a caracter in a VBA string
Post by: Keith™ 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 ...
Title: Re: Replacing a caracter in a VBA string
Post by: Guest on August 27, 2007, 02:42:01 PM

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


Woohoo!!
Title: Re: Replacing a caracter in a VBA string
Post by: Keith™ 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>
Title: Re: Replacing a caracter in a VBA string
Post by: David Hall 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
Title: Re: Replacing a caracter in a VBA string
Post by: Maverick® 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
Title: Re: Replacing a caracter in a VBA string
Post by: iliekater on August 27, 2007, 06:27:46 PM
Thanks alot guys . It's going to be helpful . :-)
Title: Re: Replacing a caracter in a VBA string
Post by: Dnereb 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