|
|
Riga 1: |
Riga 1: |
− | E' possibile sovrascrivere una parte ben determinata di una stringa con uno o più caratteri, a cominciare da una specificata posizione, utilizzando alcune funzioni della libreria esterna ''libglib-2.0'' .
| + | #REDIRECT [[Sovrascrivere_una_stringa_con_uno_o_più_caratteri#Uso_delle_funzioni_esterne_del_API_di_Libglib-2.0]] |
− | | |
− | Sarà necessario aver installato nel sistema e richiamare nell'applicazione Gambas la libreria: "''libglib-2.0.so''"
| |
− | | |
− | | |
− | Mostriamo un esempio, nel quale una stringa preesistente verrà sovrascritta con la parola "GAMBAS" a partire dal 5° carattere (num. indice 4) della stringa medesima:
| |
− | Library "libglib-2.0"
| |
− |
| |
− | Public Struct GString
| |
− | gstr As Pointer
| |
− | len As Long
| |
− | allocated_len As Long
| |
− | End Struct
| |
− |
| |
− | <FONT Color=gray>' ''GString * g_string_new (const gchar *init)''
| |
− | ' ''Creates a new GString, initialized with the given string.''</font>
| |
− | Private Extern g_string_new(init As String) As GString
| |
− |
| |
− | <FONT Color=gray>' ''GString * g_string_overwrite (GString *string, gsize pos, const gchar *val)''
| |
− | ' ''Overwrites part of a string, lengthening it if necessary.''</font>
| |
− | Private Extern g_string_overwrite(gstr As GString, gpos As Integer, gval As String) As GString
| |
− |
| |
− | <FONT Color=gray>' ''gchar * g_string_free (GString *string, gboolean free_segment)''
| |
− | ' ''Frees the memory allocated for the GString.''</font>
| |
− | Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String
| |
− |
| |
− |
| |
− | '''Public''' Sub Main()
| |
− |
| |
− | Dim s As String
| |
− | Dim gs As GString
| |
− |
| |
− | s = "abcde fghilm nopqrstuvz"
| |
− |
| |
− | gs = g_string_new(s)
| |
− |
| |
− | gs = g_string_overwrite(gs, 4, "GAMBAS")
| |
− |
| |
− | Print String@(gs.gstr)
| |
− |
| |
− | <FONT Color=gray>' ''Va in chiusura liberando memoria occupata dalla Struttura "GString":''</font>
| |
− | g_string_free(gs, True)
| |
− |
| |
− | '''End'''
| |
− | | |
− | | |
− | ====Sovrascrivere una stringa con una determinata quantità di caratteri di un'altra stringa====
| |
− | E' anche possibile sovrascrivere una stringa originaria con un numero determinato di caratteri, iniziando da sinistra, facendit parte di un'altra stringa.
| |
− | | |
− | Mostriamo un esempio, nel quale una stringa verrà sovrascritta a partire dalla posizione di indice 6 con i soli primi tre caratteri della stringa "GAMBAS":
| |
− | Library "libglib-2.0"
| |
− |
| |
− | Public Struct GString
| |
− | gstr As Pointer
| |
− | len As Long
| |
− | allocated_len As Long
| |
− | End Struct
| |
− |
| |
− | <FONT Color=gray>' ''GString * g_string_new (const gchar *init)''
| |
− | ' ''Creates a new GString, initialized with the given string.''</font>
| |
− | Private Extern g_string_new(init As String) As GString
| |
− |
| |
− | <FONT Color=gray>' ''GString * g_string_overwrite_len (GString *string, gsize pos, const gchar *val, len As Long)''
| |
− | ' ''Overwrites part of a string, lengthening it if necessary.''</font>
| |
− | Private Extern g_string_overwrite_len(gstr As GString, gpos As Integer, gval As String, llen As Long) As GString
| |
− |
| |
− | <FONT Color=gray>' ''gchar * g_string_free (GString *string, gboolean free_segment)''
| |
− | ' ''Frees the memory allocated for the GString.''</font>
| |
− | Private Extern g_string_free(gstr As GString, free_segment As Boolean) As String
| |
− |
| |
− |
| |
− | '''Public''' Sub Main()
| |
− |
| |
− | Dim s As String
| |
− | Dim gs As GString
| |
− |
| |
− | s = "abcde fghilm nopqrstuvz"
| |
− |
| |
− | gs = g_string_new(s)
| |
− |
| |
− | gs = g_string_overwrite_len(gs, 6, "GAMBAS", 3)
| |
− |
| |
− | Print String@(gs.gstr)
| |
− |
| |
− | <FONT Color=gray>' ''Va in chiusura liberando memoria occupata dalla Struttura "GString":''</font>
| |
− | g_string_free(gs, True)
| |
− |
| |
− | '''End'''
| |