Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Manipulation!!

Status
Not open for further replies.

MrVB50au

Programmer
Mar 16, 2003
326
AU
I have a string in which I want to cut off the last 3 characters and add 3 new characters.

EXAMPLE:
From:
C:\Windows\Temp\Demo.Txt
To:
C:\Windows\Temp\Demo.New

I know this is a simple task but for the life of me I have completely forgotten.

If anyone could help me out, this would be very much appreciated.

Thanks,

Andrew.
 
Try this:

strNew = Left(strOld, Len(strOld) - 3) & "new"

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 

DrJavaJoe,...

While that may work for files with just 3 letters for an extension what about files that have 4 letter extensions (jpeg)?
[tt]
S = Left(S, InStrRev(S, "."))
[/tt]

Just a more versatile version.

Good Luck

 
Good point I guess you would also want to make sure that it had an extension in the first place.

If InStr(s, ".") <> 0 Then
s = Left(s, InStrRev(s, ".")) & "new"
Else
s = s & ".new"
End If

&quot;Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.&quot;
 
Where's strongm with his RegExp solution? :)

&quot;Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.&quot;
 
Oh...hush up...

Code:
Private Function ChangeExtension(strSource As String, strNewExt As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = "(^.+\.)(\w+)($)"
        ChangeExtension = .Replace(strSource, "$1" & strNewExt & "$3")
    End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top