Remplacement d'un caractère

Résolu
dsigmoun Messages postés 135 Date d'inscription jeudi 31 juillet 2008 Statut Membre Dernière intervention 25 février 2023 - 29 juil. 2009 à 15:57
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 - 30 juil. 2009 à 10:43
Bonjour,

J'ai un string qui correspond à un code html

Dans ce code, j'ai besoin de remplacer à un endroit (un lien http:// vers une image), les espaces par "_"

J'utilise la fonction suivante pour rechercher dans le string et je remplace par replace, mais cela ne fonctionne pas.

Quelqu'un a-t'il une idée ?

mon_string = Replace(MidStr(mon_string, "http://", ">"), " ", "_")


Private Function MidStr(ByVal Str As String, ByVal sStart As String, ByVal sEnd As String, Optional ByVal Start As Integer = 30) As String
Try
Dim iStart As Integer = Str.IndexOf(sStart, Start) + sStart.Length.ToString
Dim iEnd As Integer = Str.IndexOf(sEnd, iStart)
Return Str.Substring(iStart, iEnd - iStart)
Catch ex As Exception
Return String.Empty
End Try
End Function

8 réponses

PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
29 juil. 2009 à 18:02
... ok

beh du coup c'est le premier snippet mais que tu utilises mal

(et en plus il est pas bon, je le modifie)

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim mon_string As String = "blabla TOUTE LA SOURCE HTML   lien   FIN"
        Dim sLink As String
        Dim iLastPosition As Integer = 0

        Do
            iLastPosition = mon_string.IndexOf("http://", iLastPosition + 1)
            sLink = MidStr(mon_string, "http://", ">", iLastPosition)
            If sLink.Contains(" ") Then
                mon_string = mon_string.Replace(sLink, sLink.Replace(" "c, "_"c))
            End If
        Loop While sLink.Contains(" ")

        MessageBox.Show(mon_string)
    End Sub

    Private Function MidStr(ByVal Expression As String, ByVal sLeft As String, ByVal sRight As String, Optional ByVal iStart As Integer = -1) As String
        Dim iPosL As Integer = Expression.IndexOf(sLeft, iStart)
        If iPosL > -1 Then
            Dim iPosR As Integer = Expression.IndexOf(sRight, iPosL + sLeft.Length)
            If iPosR = -1 Then
                'pas le caractère de fin, on prend la chaîne complète
                iPosR = Expression.Length - iPosL - sLeft.Length
                sRight = String.Empty
            End If
            Return Expression.Substring(iPosL, iPosR - iPosL)
        End If
        Return String.Empty
    End Function
3
Rejoignez-nous