Vb / vb.net : renvoyer ce qu'il y a avant une string, apres une string ou entre deux strings, dans une string


Contenu du snippet

Ouais bon comme l'enormement long titre le dit, ces trois fonctions vous renvoient ce qu'il y à avant un string, apres une string, ou entre deux string.
C'est super simple mais ca peut servir aux debutants...

Update : Equivalent en vb.net

Source / Exemple :


'pour lire ce qu'il y a avant la variable str
Public Function hb_avant_str(source As String, str As String)
If InStr(1, source, str) > 0 Then
hb_avant_str = Mid(source, 1, InStr(1, source, str) - 1)
Else
MsgBox "La chaine de characteres n'a pas été trouvée"
End If
End Function

'pour lire ce qu'il y a apres la variable str
Public Function hb_apres_str(source As String, str As String)
If InStr(1, source, str) > 0 Then
hb_apres_str = Mid(source, InStr(1, source, str) + Len(str), Len(source))
Else
MsgBox "La chaine de characteres n'a pas été trouvée"
End If
End Function

'Pour lire ce qu'il y a entre les variables stravant et strapres
Public Function hb_str_in_str(source As String, stravant As String, strapres As String)
If InStr(1, source, stravant) > 0 Then
If InStr((InStr(1, source, stravant)), source, strapres) > 0 Then
If Mid(source, (InStr(1, source, stravant) + Len(stravant)), InStr((InStr(1, source, stravant) + Len(stravant)), source, strapres) - (InStr(1, source, stravant) + Len(stravant))) <> "" Then
hb_str_in_str = Mid(source, (InStr(1, source, stravant) + Len(stravant)), InStr((InStr(1, source, stravant) + Len(stravant)), source, strapres) - (InStr(1, source, stravant) + Len(stravant)))
Else
MsgBox "Il n'y a rien entre les deux chaines"
End If
Else
MsgBox "la deuxieme chaine est inexistante"
End If
Else
MsgBox "La premiere chaine de characteres n'a pas été trouvée"
End If
End Function

'Voila pour appeler les fonction : votrestring = lafonction(lesargument,lesautres,...) 

'------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------
'Update : Vb.Net :

    'pour lire ce qu'il y a avant la variable str
    Public Function before_str(ByVal source As String, ByVal str As String)
        If source.IndexOf(str, 0) > 0 Then
            Return source.Substring(0, source.IndexOf(str, 0) - 1)
        End If
    End Function

    'pour lire ce qu'il y a apres la variable str
    Public Function after_str(ByVal source As String, ByVal str As String)
        If source.IndexOf(str, 0) > 0 Then
            Return source.Substring(source.IndexOf(str, 0) + str.Length)
        End If
    End Function

    Public Function between_str(ByVal StartStr As String, ByVal str1 As String, ByVal str2 As String) As String
        If StartStr.IndexOf(str1, 0) >= 0 Then
            If StartStr.IndexOf(str2, StartStr.IndexOf(str1, 0) + str1.Length) >= 0 Then
                Return StartStr.Substring(StartStr.IndexOf(str1, 0) + str1.Length, StartStr.IndexOf(str2, StartStr.IndexOf(str1) + str1.Length) - StartStr.IndexOf(str1) - str1.Length) 'InStr((InStr(1, startStr, str1) + Len(str1)), startStr, str2) - (InStr(1, startStr, str1) + Len(str1)))
            End If
        End If
    End Function

Conclusion :


c'est bon, c'est simple certe, mais ca peut servir...alors les critiques non constructives, vous pouvez les garder.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.