Jul Pod
Jul Pod

Reputation: 396

Cut String at Special Position VBA

I got this string here: \server\Documents\test\\954076 how i check if there is a \ a second time and how i cut it then off? So that \server\Documents\test\\954076 is gonna be \server\Documents\test\954076

Upvotes: 1

Views: 91

Answers (2)

user3598756
user3598756

Reputation: 29421

if you're worried about double repetitions then it suffices

Dim strng As String

strng = "\server\Documents\test\\\954076"

strng = Replace(strng, "\\", "\")

if you want to handle multiple repetitions then:

Dim strng As String

strng = "\server\Documents\test\\\954076"

Do While Len(strng) - Len(Replace(strng, "\\", "\")) > 0
    strng = Replace(strng, "\\", "\")
Loop

Upvotes: 1

A.S.H
A.S.H

Reputation: 29332

Here's a general solution to reduce any consecutive repetitions of a given character:

Sub RemoveRepetitions(s As String, c As String)
    Dim len1 As Long, len2 As Long
    Do
        len1 = Len(s)
        s = Replace(s, c & c, c)
        len2 = Len(s)
    Loop Until len2 = len1
End Sub

Sub testing()
    Dim s As String: s = "\\server\\\\\\Documents\\\\\test\\954076"
    RemoveRepetitions s, "\"
    Debug.Print s
End Sub

\server\Documents\test\954076

Upvotes: 1

Related Questions