Individuare in una stringa i caratteri ricorrenti e loro posizioni

Da Gambas-it.org - Wikipedia.

Per poter individuare in una stringa i caratteri ricorrenti e loro rispettive posizioni, possiamo adottare varie modalità.

Ne mostriamo alcune.

1a modalità

Public Sub Main()
 
 Dim s As String = "abcdabcdabcd abcd abcd"
 Dim i As Integer
 
  For i = 1 To String.Len(s)
    If Mid(s, i, 1) = "a" Then Print i, "a"
  Next
  
End


2a modalità

Public Sub Main()
 
 Dim s As String = "abcdabcdabcd abcd abcd"
 Dim bb As Byte[]
 Dim i As Integer
 
  bb = Byte[].FromString(s)
   
  For i = 0 To bb.Max
    If bb[i] = 97 Then Print i + 1, "a"
' oppure: If bb[i] = 97 Then Print i + 1, bb.ToString(i, 1)
' oppure: If bb.ToString(i,1) = "a" Then Print i + 1, bb.ToString(i, 1)
  Next
  
End


3a modalità

Public Sub Main()
 
 Dim s As String = "abcdabcdabcd abcd abcd"
 Dim bb As Byte[]
 Dim i As Integer
 
  bb = Byte[].FromString(s)
   
  Do
    If Byte@(bb.Data + i) = 97 Then Print i + 1, "a"
' oppure: If bb[i] = 97 Then Print i + 1, bb.ToString(i, 1)
' oppure: If bb.ToString(i,1) = "a" Then Print i + 1, bb.ToString(i, 1)
    Inc i
  Loop Until Byte@(bb.Data + i) = 0
  
End