## Strings Strings — text buffers which grow automatically as text is added |

## Functions

GString * | g_string_new ()
GString * | g_string_new_len ()
GString * | g_string_sized_new ()
GString * | g_string_assign ()
void | g_string_vprintf ()
void | g_string_append_vprintf ()
void | g_string_printf ()
void | g_string_append_printf ()
GString * | g_string_append ()
GString * | g_string_append_c ()
GString * | g_string_append_unichar ()
GString * | g_string_append_len ()
GString * | g_string_append_uri_escaped ()
GString * | g_string_prepend ()
GString * | g_string_prepend_c ()
GString * | g_string_prepend_unichar ()
GString * | g_string_prepend_len ()
GString * | g_string_insert ()
GString * | g_string_insert_c ()
GString * | g_string_insert_unichar ()
GString * | g_string_insert_len ()
GString * | g_string_overwrite ()
GString * | g_string_overwrite_len ()
guint | g_string_replace ()
GString * | g_string_erase ()
GString * | g_string_truncate ()
GString * | g_string_set_size ()
gchar * | g_string_free ()
GBytes * | g_string_free_to_bytes ()
GString * | g_string_up ()
GString * | g_string_down ()
guint | g_string_hash ()
gboolean | g_string_equal ()

## Types and Values

struct | GString
#define | g_string_sprintf
#define | g_string_sprintfa

## Includes

    #include <gmodule.h>

## Description

A GString is an object that handles the memory management of a C string for you. The emphasis of GString is on text, typically UTF-8. Crucially, the "str" member of a GString is guaranteed to have a trailing nul character, and it is therefore always safe to call functions such as `strchr()` or `g_strdup()` on it.

However, a GString can also hold arbitrary binary data, because it has a "len" member, which includes any possible embedded nul characters in the data. Conceptually then, GString is like a GByteArray with the addition of many convenience methods for text, and a guaranteed nul terminator.

## Functions

### g_string_new ()

    GString *
    g_string_new (const gchar *init);

Creates a new GString, initialized with the given string.

[constructor]

#### Parameters

init | the initial text to copy into the string, or `NULL` to start with an empty string. | [nullable]

#### Returns

the new GString.

[transfer full]

### g_string_new_len ()

    GString *
    g_string_new_len (const gchar *init,
                      gssize len);

Creates a new GString with `len` bytes of the `init` buffer. Because a length is provided, `init` need not be nul-terminated, and can contain embedded nul bytes.

Since this function does not stop at nul bytes, it is the caller's responsibility to ensure that `init` has at least `len` addressable bytes.

[constructor]

#### Parameters

init | initial contents of the string |
len | length of `init` to use |

#### Returns

a new GString.

[transfer full]

### g_string_sized_new ()

    GString *
    g_string_sized_new (gsize dfl_size);

Creates a new GString, with enough space for `dfl_size` bytes. This is useful if you are going to add a lot of text to the string and don't want it to be reallocated too often.

[constructor]

#### Parameters

dfl_size | the default size of the space allocated to hold the string |

#### Returns

the new GString.

[transfer full]

### g_string_assign ()

    GString *
    g_string_assign (GString *string,
                     const gchar *rval);

Copies the bytes from a string into a GString, destroying any previous contents. It is rather like the standard `strcpy()` function, except that you do not have to worry about having enough space to copy the string.

#### Parameters

string | the destination GString. Its current contents are destroyed. |
rval | the string to copy into `string` |

#### Returns

`string` .

[transfer none]

### g_string_vprintf ()

    void
    g_string_vprintf (GString *string,
                      const gchar *format,
                      va_list args);

Writes a formatted string into a GString. This function is similar to `g_string_printf()` except that the arguments to the format string are passed as a va_list.

#### Parameters

string | a GString |
format | the string format. See the `printf()` documentation. | [not nullable]
args | the parameters to insert into the format string |

Since: 2.14

### g_string_append_vprintf ()

    void
    g_string_append_vprintf (GString *string,
                             const gchar *format,
                             va_list args);

Appends a formatted string onto the end of a GString. This function is similar to `g_string_append_printf()` except that the arguments to the format string are passed as a va_list.

#### Parameters

string | a GString |
format | the string format. See the `printf()` documentation. | [not nullable]
args | the list of arguments to insert in the output |

Since: 2.14

### g_string_printf ()

    void
    g_string_printf (GString *string,
                     const gchar *format,
                     ...);

Writes a formatted string into a GString. This is similar to the standard `sprintf()` function, except that the GString buffer automatically expands to contain the results. The previous contents of the GString are destroyed.

#### Parameters

string | a GString |
format | the string format. See the `printf()` documentation |
... | the parameters to insert into the format string |

### g_string_append_printf ()

    void
    g_string_append_printf (GString *string,
                            const gchar *format,
                            ...);

Appends a formatted string onto the end of a GString. This function is similar to `g_string_printf()` except that the text is appended to the GString.

#### Parameters

string | a GString |
format | the string format. See the `printf()` documentation |
... | the parameters to insert into the format string |

### g_string_append ()

    GString *
    g_string_append (GString *string,
                     const gchar *val);

Adds a string onto the end of a GString, expanding it if necessary.

#### Parameters

string | a GString |
val | the string to append onto the end of `string` |

#### Returns

`string` .

[transfer none]

### g_string_append_c ()

    GString *
    g_string_append_c (GString *string,
                       gchar c);

Adds a byte onto the end of a GString, expanding it if necessary.

#### Parameters

string | a GString |
c | the byte to append onto the end of `string` |

#### Returns

`string` .

[transfer none]

### g_string_append_unichar ()

    GString *
    g_string_append_unichar (GString *string,
                             gunichar wc);

Converts a Unicode character into UTF-8, and appends it to the string.

#### Parameters

string | a GString |
wc | a Unicode character |

#### Returns

`string` .

[transfer none]

### g_string_append_len ()

    GString *
    g_string_append_len (GString *string,
                         const gchar *val,
                         gssize len);

Appends `len` bytes of `val` to `string` .

If `len` is positive, `val` may contain embedded nuls and need not be nul-terminated. It is the caller's responsibility to ensure that `val` has at least `len` addressable bytes.

If `len` is negative, `val` must be nul-terminated and `len` is considered to request the entire string length. This makes `g_string_append_len()` equivalent to `g_string_append()`.

#### Parameters

string | a GString |
val | bytes to append |
len | number of bytes of `val` to use, or -1 for all of `val` |

#### Returns

`string` .

[transfer none]

### g_string_append_uri_escaped ()

    GString *
    g_string_append_uri_escaped (GString *string,
                                 const gchar *unescaped,
                                 const gchar *reserved_chars_allowed,
                                 gboolean allow_utf8);

Appends `unescaped` to `string` , escaping any characters that are reserved in URIs using URI-style escape sequences.

#### Parameters

string | a GString |
unescaped | a string |
reserved_chars_allowed | a string of reserved characters allowed to be used, or `NULL` |
allow_utf8 | set `TRUE` if the escaped string may include UTF8 characters |

#### Returns

`string` .

[transfer none]

Since: 2.16

### g_string_prepend ()

    GString *
    g_string_prepend (GString *string,
                      const gchar *val);

Adds a string on to the start of a GString, expanding it if necessary.

#### Parameters

string | a GString |
val | the string to prepend on the start of `string` |

#### Returns

`string` .

[transfer none]

### g_string_prepend_c ()

    GString *
    g_string_prepend_c (GString *string,
                        gchar c);

Adds a byte onto the start of a GString, expanding it if necessary.

#### Parameters

string | a GString |
c | the byte to prepend on the start of the GString |

#### Returns

`string` .

[transfer none]

### g_string_prepend_unichar ()

    GString *
    g_string_prepend_unichar (GString *string,
                              gunichar wc);

Converts a Unicode character into UTF-8, and prepends it to the string.

#### Parameters

string | a GString |
wc | a Unicode character |

#### Returns

`string` .

[transfer none]

### g_string_prepend_len ()

    GString *
    g_string_prepend_len (GString *string,
                          const gchar *val,
                          gssize len);

Prepends `len` bytes of `val` to `string` .

If `len` is positive, `val` may contain embedded nuls and need not be nul-terminated. It is the caller's responsibility to ensure that `val` has at least `len` addressable bytes.

If `len` is negative, `val` must be nul-terminated and `len` is considered to request the entire string length. This makes `g_string_prepend_len()` equivalent to `g_string_prepend()`.

#### Parameters

string | a GString |
val | bytes to prepend |
len | number of bytes in `val` to prepend, or -1 for all of `val` |

#### Returns

`string` .

[transfer none]

### g_string_insert ()

    GString *
    g_string_insert (GString *string,
                     gssize pos,
                     const gchar *val);

Inserts a copy of a string into a GString, expanding it if necessary.

#### Parameters

string | a GString |
pos | the position to insert the copy of the string |
val | the string to insert |

#### Returns

`string` .

[transfer none]

### g_string_insert_c ()

    GString *
    g_string_insert_c (GString *string,
                       gssize pos,
                       gchar c);

Inserts a byte into a GString, expanding it if necessary.

#### Parameters

string | a GString |
pos | the position to insert the byte |
c | the byte to insert |

#### Returns

`string` .

[transfer none]

### g_string_insert_unichar ()

    GString *
    g_string_insert_unichar (GString *string,
                             gssize pos,
                             gunichar wc);

Converts a Unicode character into UTF-8, and insert it into the string at the given position.

#### Parameters

string | a GString |
pos | the position at which to insert character, or -1 to append at the end of the string |
wc | a Unicode character |

#### Returns

`string` .

[transfer none]

### g_string_insert_len ()

    GString *
    g_string_insert_len (GString *string,
                         gssize pos,
                         const gchar *val,
                         gssize len);

Inserts `len` bytes of `val` into `string` at `pos` .

If `len` is positive, `val` may contain embedded nuls and need not be nul-terminated. It is the caller's responsibility to ensure that `val` has at least `len` addressable bytes.

If `len` is negative, `val` must be nul-terminated and `len` is considered to request the entire string length.

If `pos` is -1, bytes are inserted at the end of the string.

#### Parameters

string | a GString |
pos | position in `string` where insertion should happen, or -1 for at the end |
val | bytes to insert |
len | number of bytes of `val` to insert, or -1 for all of `val` |

#### Returns

`string` .

[transfer none]

### g_string_overwrite ()

    GString *
    g_string_overwrite (GString *string,
                        gsize pos,
                        const gchar *val);

Overwrites part of a string, lengthening it if necessary.

#### Parameters

string | a GString |
pos | the position at which to start overwriting |
val | the string that will overwrite the `string` starting at `pos` |

#### Returns

`string` .

[transfer none]

Since: 2.14

### g_string_overwrite_len ()

    GString *
    g_string_overwrite_len (GString *string,
                            gsize pos,
                            const gchar *val,
                            gssize len);

Overwrites part of a string, lengthening it if necessary. This function will work with embedded nuls.

#### Parameters

string | a GString |
pos | the position at which to start overwriting |
val | the string that will overwrite the `string` starting at `pos` |
len | the number of bytes to write from `val` |

#### Returns

`string` .

[transfer none]

Since: 2.14

### g_string_replace ()

    guint
    g_string_replace (GString *string,
                      const gchar *find,
                      const gchar *replace,
                      guint limit);

Replaces the string `find` with the string `replace` in a GString up to `limit` times. If the number of instances of `find` in the GString is less than `limit` , all instances are replaced. If `limit` is `0`, all instances of `find` are replaced.

If `find` is the empty string, since versions 2.69.1 and 2.68.4 the replacement will be inserted no more than once per possible position (beginning of string, end of string and between characters). This did not work correctly in earlier versions.

#### Parameters

string | a GString |
find | the string to find in `string` |
replace | the string to insert in place of `find` |
limit | the maximum instances of `find` to replace with `replace` , or `0` for no limit |

#### Returns

the number of find and replace operations performed.

Since: 2.68

### g_string_erase ()

    GString *
    g_string_erase (GString *string,
                    gssize pos,
                    gssize len);

Removes `len` bytes from a GString, starting at position `pos` . The rest of the GString is shifted down to fill the gap.

#### Parameters

string | a GString |
pos | the position of the content to remove |
len | the number of bytes to remove, or -1 to remove all following bytes |

#### Returns

`string` .

[transfer none]

### g_string_truncate ()

    GString *
    g_string_truncate (GString *string,
                       gsize len);

Cuts off the end of the GString, leaving the first `len` bytes.

#### Parameters

string | a GString |
len | the new size of `string` |

#### Returns

`string` .

[transfer none]

### g_string_set_size ()

    GString *
    g_string_set_size (GString *string,
                       gsize len);

Sets the length of a GString. If the length is less than the current length, the string will be truncated. If the length is greater than the current length, the contents of the newly added area are undefined. (However, as always, string->str[string->len] will be a nul byte.)

#### Parameters

string | a GString |
len | the new length |

#### Returns

`string` .

[transfer none]

### g_string_free ()

    gchar *
    g_string_free (GString *string,
                   gboolean free_segment);

Frees the memory allocated for the GString. If `free_segment` is `TRUE` it also frees the character data. If it's `FALSE`, the caller gains ownership of the buffer and must free it after use with `g_free()`.

#### Parameters

string | a GString. | [transfer full]
free_segment | if `TRUE`, the actual character data is freed as well |

#### Returns

the character data of `string` (i.e. `NULL` if `free_segment` is `TRUE`).

[nullable]

### g_string_free_to_bytes ()

    GBytes *
    g_string_free_to_bytes (GString *string);

Transfers ownership of the contents of `string` to a newly allocated GBytes. The GString structure itself is deallocated, and it is therefore invalid to use `string` after invoking this function.

Note that while GString ensures that its buffer always has a trailing nul character (not reflected in its "len"), the returned GBytes does not include this extra nul; i.e. it has length exactly equal to the "len" member.

#### Parameters

string | a GString. | [transfer full]

#### Returns

A newly allocated GBytes containing contents of `string` ; `string` itself is freed.

[transfer full]

Since: 2.34

### g_string_up ()

    GString *
    g_string_up (GString *string);

`g_string_up` has been deprecated since version 2.2 and should not be used in newly-written code.

This function uses the locale-specific `toupper()` function, which is almost never the right thing. Use `g_string_ascii_up()` or `g_utf8_strup()` instead.

Converts a GString to uppercase.

#### Parameters

string | a GString |

#### Returns

`string` .

[transfer none]

### g_string_down ()

    GString *
    g_string_down (GString *string);

`g_string_down` has been deprecated since version 2.2 and should not be used in newly-written code.

This function uses the locale-specific `tolower()` function, which is almost never the right thing. Use `g_string_ascii_down()` or `g_utf8_strdown()` instead.

Converts a GString to lowercase.

#### Parameters

string | a GString |

#### Returns

the GString.

[transfer none]

### g_string_hash ()

    guint
    g_string_hash (const GString *str);

Creates a hash code for `str` ; for use with GHashTable.

#### Parameters

str | a string to hash |

#### Returns

hash code for `str`

### g_string_equal ()

    gboolean
    g_string_equal (const GString *v,
                    const GString *v2);

Compares two strings for equality, returning `TRUE` if they are equal. For use with GHashTable.

#### Parameters

v | a GString |
v2 | another GString |

#### Returns

`TRUE` if the strings are the same length and contain the same bytes

## Types and Values

### struct GString

    struct GString {
      gchar  *str;
      gsize len;
      gsize allocated_len;
    };

The GString struct contains the public fields of a GString.

#### Members

gchar *`str`; | points to the character data. It may move as text is added. The `str` field is null-terminated and so can be used as an ordinary C string. |
gsize `len`; | contains the length of the string, not including the terminating nul byte. |
gsize `allocated_len`; | the number of bytes that can be stored in the string before it needs to be reallocated. May be larger than `len` . |

### g_string_sprintf

    #define             g_string_sprintf

`g_string_sprintf` is deprecated and should not be used in newly-written code.

This function has been renamed to `g_string_printf()`.

Writes a formatted string into a GString. This is similar to the standard `sprintf()` function, except that the GString buffer automatically expands to contain the results. The previous contents of the GString are destroyed.

#### Parameters

string | a GString |
format | the string format. See the `sprintf()` documentation |
... | the parameters to insert into the format string |

### g_string_sprintfa

    #define             g_string_sprintfa

`g_string_sprintfa` is deprecated and should not be used in newly-written code.

This function has been renamed to `g_string_append_printf()`

Appends a formatted string onto the end of a GString. This function is similar to `g_string_sprintf()` except that the text is appended to the GString.

#### Parameters

string | a GString |
format | the string format. See the `sprintf()` documentation |
... | the parameters to insert into the format string |