Compter le nombre d'un caractère dans une string

Jeronimo - 28 mars 2001 à 07:53
 VB_MAN - 28 mars 2001 à 08:10
Je dois trouver le nombre d'un caractère dans une chaîne de caractère

strCar=a
strTexte =txtTexte.text

trouver le nombre de strCar dans strTexte
J'ai essayé la méthode de VB_MAN dans sa réponse à "compter les lettres" , mais ca ne fonctionne pas !

Comment faire ?

3 réponses

c'est ok ! Une inattention de ma part
0
Bien pour savoir combien de "a"(ou autres) il y à dans une chaine de caractère tu peut faire comme ca:

Private Sub Command1_Click()

Dim lStart As Long
Dim StrTexte As String
Dim StrCar As String
Dim NumCar As Long

On Error Resume Next

StrTexte = "Allo ca va bien"
StrCar = "a"
NumCar = 0

lStart = InStr(1, LCase$(StrTexte), LCase$(StrCar))

If lStart > 0 Then

NumCar = 1

Do While lStart > 0

lStart = InStr(lStart + 1, LCase$(StrTexte), LCase$(StrCar))

If lStart > 0 Then NumCar = NumCar + 1

Loop

End If

MsgBox "Il y à " & NumCar & " " & StrCar & " dans : Allo ca va bien", vbInformation

End Sub

Je l'ai essayer et ca fonctionne bien.

NitRic28@Hotmail.com

VB_MAN
0
Bien pour savoir combien de "a"(ou autres) il y à dans une chaine de caractère tu peut faire comme ca:

Private Sub Command1_Click()

Dim lStart As Long
Dim StrTexte As String
Dim StrCar As String
Dim NumCar As Long

On Error Resume Next

StrTexte = "Allo ca va bien"
StrCar = "a"
NumCar = 0

lStart = InStr(1, LCase$(StrTexte), LCase$(StrCar))

If lStart > 0 Then

NumCar = 1

Do While lStart > 0

lStart = InStr(lStart + 1, LCase$(StrTexte), LCase$(StrCar))

If lStart > 0 Then NumCar = NumCar + 1

Loop

End If

MsgBox "Il y à " & NumCar & " " & StrCar & " dans : Allo ca va bien", vbInformation

End Sub

Je l'ai essayer et ca fonctionne bien.

NitRic28@Hotmail.com

VB_MAN
0
Rejoignez-nous