Differenze tra le versioni di "Individuare in una stringa i caratteri ricorrenti e loro posizioni"
Da Gambas-it.org - Wikipedia.
Riga 51: | Riga 51: | ||
Inc i | Inc i | ||
Loop Until Byte@(bb.Data + i) = 0 | Loop Until Byte@(bb.Data + i) = 0 | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | |||
+ | ====4<SUP>a</sup> modalità==== | ||
+ | Una leggera variante della precedente modalità: | ||
+ | '''Public''' Sub Main() | ||
+ | |||
+ | Dim s As String = "abcdabcdabcd abcd abcd" | ||
+ | Dim p As Pointer | ||
+ | Dim i As Integer | ||
+ | |||
+ | p = Alloc(s) | ||
+ | |||
+ | Do | ||
+ | If Byte@(p + i) = 97 Then Print i + 1, "a" | ||
+ | Inc i | ||
+ | Loop Until Byte@(p + i) = 0 | ||
'''End''' | '''End''' |
Versione delle 14:20, 4 mar 2016
Per poter individuare in una stringa i caratteri ricorrenti e loro rispettive posizioni, possiamo adottare varie modalità.
Ne mostriamo alcune, nelle quali si intende individuare il carattere "a" in una stringa di testo, e, per ciascun carattere "a" individuato, la sua posizione.
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
4a modalità
Una leggera variante della precedente modalità:
Public Sub Main() Dim s As String = "abcdabcdabcd abcd abcd" Dim p As Pointer Dim i As Integer p = Alloc(s) Do If Byte@(p + i) = 97 Then Print i + 1, "a" Inc i Loop Until Byte@(p + i) = 0 End