Differenze tra le versioni di "Verificare se in una stringa vi sono caratteri non stampabili"

Da Gambas-it.org - Wikipedia.
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 2: Riga 2:
 
<BR>Ricordiamo che un carattere stampabile non è un ''carattere di controllo'', e pertanto è compreso fra il num. 32 ed il num. 126 del codice ASCII.
 
<BR>Ricordiamo che un carattere stampabile non è un ''carattere di controllo'', e pertanto è compreso fra il num. 32 ed il num. 126 del codice ASCII.
  
Potremo utilizzare, nel frattempo, la funzione [[Isprint()|''IsPrint()'']] del linguaggio C.
+
Sarà possibile adottare una modalità che prevede l'uso delle sole risorse di Gambas, come l'esempio di codice che segue: <SUP>&#091;[[#Note|nota 1]]&#093;</sup>
 +
Public Sub Main()
 +
 +
  Dim s As String
 +
  Dim b As Byte
 +
  Dim v As Boolean
 +
 +
  s = "Testo\nqualsiasi"
 +
   
 +
  For b = 1 To String.Len(s) - 1
 +
    Select Case Asc(s, b)
 +
      Case 31 To 126
 +
        v = False
 +
      Case Else
 +
        v = True
 +
        Break
 +
    End Select
 +
  Next
 +
   
 +
  Print v
 +
   
 +
End
  
 
+
oppure utilizzare la funzione "[[Isprint()|IsPrint()]]" del linguaggio C:
Mostriamo un possibile esempio:
 
 
  <FONT Color=gray>' ''int isprint(int c)''
 
  <FONT Color=gray>' ''int isprint(int c)''
 
  ' ''Check whether the passed character is printable.''</font>
 
  ' ''Check whether the passed character is printable.''</font>
Riga 11: Riga 31:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
 
   Dim s As String = "Testo\nqualsiasi"
 
   Dim s As String = "Testo\nqualsiasi"
Riga 17: Riga 37:
 
   Dim b As Byte
 
   Dim b As Byte
 
   
 
   
  bb = Byte[].FromString(s)
+
  bb = Byte[].FromString(s)
 
    
 
    
    For Each b In bb
+
  For Each b In bb
      Print CBool(isprint(CInt(b)))
+
    Print CBool(isprint(CInt(b)))
    Next
+
  Next
 
    
 
    
  '''End'''
+
  End
 +
 
 +
 
 +
 
 +
=Note=
 +
[1] Vedere anche le seguenti pagine:
 +
* [[Rimuovere da una stringa tutti i caratteri non-stampabili eventualmente presenti]]
 +
* [[Non visualizzare i caratteri non-stampabili di una stringa che inizia con caratteri stampabili]]

Versione attuale delle 08:31, 24 giu 2023

In Gambas attualmente non esiste una specifica funzione per sapere se un carattere è stampabile o meno.
Ricordiamo che un carattere stampabile non è un carattere di controllo, e pertanto è compreso fra il num. 32 ed il num. 126 del codice ASCII.

Sarà possibile adottare una modalità che prevede l'uso delle sole risorse di Gambas, come l'esempio di codice che segue: [nota 1]

Public Sub Main()

 Dim s As String
 Dim b As Byte
 Dim v As Boolean

 s = "Testo\nqualsiasi"
   
 For b = 1 To String.Len(s) - 1
   Select Case Asc(s, b)
     Case 31 To 126
       v = False
     Case Else
       v = True
       Break
   End Select
 Next
   
 Print v
   
End

oppure utilizzare la funzione "IsPrint()" del linguaggio C:

' int isprint(int c)
' Check whether the passed character is printable.
Private Extern isprint(num As Integer) As Integer In "libc:6"


Public Sub Main()

 Dim s As String = "Testo\nqualsiasi"
 Dim bb As Byte[]
 Dim b As Byte

 bb = Byte[].FromString(s)
  
 For Each b In bb
   Print CBool(isprint(CInt(b)))
 Next
 
End


Note

[1] Vedere anche le seguenti pagine: