List Info

Thread: How to replace all occurences of character within a section of string.




How to replace all occurences of character within a section of string.
user name
2006-03-11 15:18:30
> I did figure out a not so pretty way of
> doing it. Oh well. I guess it does work...
>
>     Private Function FixSlashInPath(ByVal thisfile As
String) As String
>         Dim iLoop As Integer
>         Dim tempString As String
>         Dim newString As String
>         For iLoop =3D 0 To thisfile.Length - 5
>             If thisfile.Substring(iLoop, 3) =3D
"src" Then
>                 tempString =3D thisfile.Substring(iLoop
+ 5,
> thisfile.IndexOf(Chr(34), iLoop + 6) - (iLoop + 5))
>                 newString =3D
tempString.Replace("\", "/")
>                 thisfile =3D
thisfile.Replace(tempString, newString)
>             End If
>         Next
>         Return thisfile
>     End Function

Here's a more regular-expression-ey attempt. It searches
for the pattern
"[^"]*" (note that the leading and
trailing quotes are part of the pattern),
which matches a quote, followed by any number of non-quotes,
followed by a
quote. The code loops through the matches making the
replacements. I
replaced / with ? in the example to better see what it was
doing.

Private Sub btnReplace_Click(ByVal sender As System.Object,
ByVal e As
System.EventArgs) Handles btnReplace.Click
    Dim input As String = txtInput.Text
    Dim result As String = ""
    Dim next_unmatched As Integer = 0

    For Each match As Match In Regex.Matches(input,
txtPattern.Text)
        ' Add the text before the next match.
        If match.Index > next_unmatched Then
            result &= input.Substring(next_unmatched,
match.Index -
next_unmatched)
        End If

        ' Add the matched text.
        result &= match.Value.Replace("/",
"?")
        ' Debug.WriteLine("[" & match.Value
& "]")

        ' Set next_unmatched to the index
        ' of the last character in this match.
        next_unmatched = match.Index + match.Length
    Next match

    ' Add the rest of the string.
    If input.Length > next_unmatched Then
        result &= input.Substring(next_unmatched)
    End If

    ' Display the result.
    txtResult.Text = result
End Sub

You can download an example at:

    http://www.vb-helper.com/howto_net_replace_regex
_substrings.html

Unfortunately it's not much better than your code, although
it looks for
matched quotes rather than relying on the "src"
in the data. I couldn't
think of a way to make it substitute within the matches
automatically. I'd
be interested to see it.

Rod

Rod Stephens, Author of "Visual Basic 2005
Programmer's Reference"
http://www.v
b-helper.com/vb_prog_ref.htm
Join the VB Helper mailing list at http://www.v
b-helper.com/newsletter.html

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )