Differenze tra le versioni di "Individuare in una stringa i caratteri ricorrenti e loro posizioni"

Da Gambas-it.org - Wikipedia.
Riga 28: Riga 28:
 
   For i = 0 To bb.Max
 
   For i = 0 To bb.Max
 
     If bb[i] = 97 Then Print i + 1, "a"
 
     If bb[i] = 97 Then Print i + 1, "a"
 +
<FONT Color=gray>' ''oppure:'' '''If bb[i] = 97 Then Print i + 1, bb.ToString(i, 1)'''</font>
 +
<FONT Color=gray>' ''oppure:'' '''If bb.ToString(i,1) = "a" Then Print i + 1, bb.ToString(i, 1)'''</font>
 
   Next
 
   Next
 
    
 
    
Riga 45: Riga 47:
 
   Do
 
   Do
 
     If Byte@(bb.Data + i) = 97 Then Print i + 1, "a"
 
     If Byte@(bb.Data + i) = 97 Then Print i + 1, "a"
 +
<FONT Color=gray>' ''oppure:'' '''If bb[i] = 97 Then Print i + 1, bb.ToString(i, 1)'''</font>
 +
<FONT Color=gray>' ''oppure:'' '''If bb.ToString(i,1) = "a" Then Print i + 1, bb.ToString(i, 1)'''</font>
 
     Inc i
 
     Inc i
 
   Loop Until Byte@(bb.Data + i) = 0
 
   Loop Until Byte@(bb.Data + i) = 0
 
    
 
    
 
  '''End'''
 
  '''End'''

Versione delle 14:04, 4 mar 2016

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