Les Snippets

Connexion

Compter chaque occurrence de caractères d'une chaîne

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 17/12/2008 13:46:14 et initié par PCPT [Liste]
Date de mise à jour : 17/12/2008 13:48:31
Vue : 5233
Catégorie(s) : Chaîne de caractères
Langages dispo pour ce code :
- VB 2005, VB 2008, VB.NET 1.x
- VB6, VBA
- Delphi 5



Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 17/12/2008
Posté par PCPT [Liste]
   Function GetCharsCount(ByVal sText As StringOptional ByVal bUcase As Boolean = FalseAs Int32()
        Dim aiRet(0 To 255As Int32
        If sText.Length > 0 Then
            If bUcase Then sText = sText.ToUpper
            Dim abChars() As Char = sText.ToCharArray
            For i As Int32 = 0 To sText.Length - 1
                aiRet(Convert.ToByte(abChars(i))) += 1
            Next
        End If
        Return aiRet
    End Function

'=====================
'EXEMPLE D'UTILISATION
'=====================
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        Dim aiChars() As Int32 = GetCharsCount(TextBox1.Text, True)
        For i As Int32 = 0 To 255
            If aiChars(i) > 0 Then ListBox1.Items.Add(Convert.ToChar(i) & "          " & aiChars(i).ToString)
        Next
    End Sub
Langage : VB6 , VBA
Date ajout : 17/12/2008
Posté par PCPT [Liste]
DateMAJ : 17/12/2008
Function GetCharsCount(ByVal sText As String, Optional ByVal bUcase As Boolean = False)  As Long()
    Dim alRet(To 255As Long, i As Long,  b As Integer
    If bUcase Then sText = UCase$(sText)
    For i = To Len(sText)
        b = Asc(Mid$(sText, i, 1))
        alRet(b) = alRet(b) + 1
    Next i
    GetCharsCount = alRet
    Erase alRet
End Function

'=====================
'EXEMPLE  D'UTILISATION
'=====================
Private Sub Command1_Click()
    Dim alChars() As Long, i As Integer
    
    List1.Clear
    alChars = GetCharsCount(Text1.Text)
    For i = To 255
        If alChars(i) > Then List1.AddItem Chr$(i) & "          " CStr(alChars(i))
    Next i
End Sub

Langage : Delphi 5
Date ajout : 18/07/2009
Posté par f0xi [Liste]
type
  TCharsCounter = array[AnsiChar] of integer;
function CountChars(const S : AnsiString; var MinRange, MaxRange: Ansichar): TCharsCounter;
var N : integer;
    C : AnsiChar;
begin
  for C := low(result) to high(result) do
    result[C] := 0;
  MinRange := #255;
  MaxRange := #0;
  for N := 1 to Length(S) do
  begin
    inc(result[S[N]]);
    if S[N] < MinRange then
      MinRange := S[N];
    if S[N] > MaxRange then
      MaxRange := S[N];
  end;
end;

Snippets en rapport avec : Caractère, Compter, Occurence, Char, Chaîne



Codes sources en rapport avec : Caractère, Compter, Occurence, Char, Chaîne

{Visual Basic, VB6, VB.NET, VB 2005} COMPTER LES MOTS DANS UNE CHAINE DE CARACTÈRE
Encore un truc tout simple mais qui peut servir ...

{ASP / ASP.NET} CHAINES DE CARACTÈRES - UPLOAD
Ce code peut servir de complément au projet de fabrice69 "FabManager" pour y intéger l'upload. De no...

{ASP / ASP.NET} GÉNÉRER UNE CHAINE ALÉATOIRE
Ce code source est très simple. Il permet de créer une chaine aléatoire (pratique pour une demande d...

{ASP / ASP.NET} DÉCOUPER UNE CHAINE DE CARACTÈRE
Voici une petite fonction qui sert à découper une chaîne de caractère, pour l'utiliser, passer en pa...

{JAVA / J2EE} PLACER LES MOTS D'UNE CHAINE DANS UN TABLEAU
Ce code se prèsente sous forme d'un pakage, alors attention à l'utilisation ! Il permet de mettre d...

{JAVA / J2EE} SCANF TOUT BETE
Voici un exemple tout bête d'un équivalent de scanf() en C. Attention c'est vraiment basique ;) ...

{PHP} REMPLACER UN CARACTÉRE DANS UN CHAINE
Une petite fonction toute simple pour remplacer une partie d'un texte par autre chose. Exemple : P...

{C / C++ / C++.NET} UPANDDOWN ( CHAINE DE CARACTÈRE )
Encore un simple petit bout de code pour manipulé une chaine, ex: char MaChaine[] = "Salut"; U...

{C / C++ / C++.NET} ADDSLASH & REMOVESLASH ( CHAINE DE CARACTÈRE )
Sous VB il y à plusieurs petit exemple qui montre comment ajouter et/ou enlever une barre oblique(\)...

{ASP / ASP.NET} CONCATÉNATION D'UN CHAÎNE DE CARACTÈRES
A priori très simple comme opération, la concaténation d'une chaîne de caractère peut parfois demand...