GLib – 2.0: String Utilities
Description: Reference for GLib-2.0: String Utilities

#### String Utilities

This section describes a number of utility functions for creating, duplicating, and manipulating strings.

Note that the functions `g_printf()`, `g_fprintf()`, `g_sprintf()`, `g_vprintf()`, `g_vfprintf()`, `g_vsprintf()` and `g_vasprintf()` are declared in the header `gprintf.h` which is not included in `glib.h` (otherwise using `glib.h` would drag in `stdio.h`), so you’ll have to explicitly include `<glib/gprintf.h>` in order to use the GLib `printf()` functions.

## String precision pitfalls # {#string-precision}

While you may use the `printf()` functions to format UTF-8 strings, notice that the precision of a `%Ns` parameter is interpreted as the number of bytes, not characters to print. On top of that, the GNU libc implementation of the `printf()` functions has the ‘feature’ that it checks that the string given for the `%Ns` parameter consists of a whole number of characters in the current encoding. So, unless you are sure you are always going to be in an UTF-8 locale or your know your text is restricted to ASCII, avoid using `%Ns`. If your intention is to format strings for a certain number of columns, then `%Ns` is not a correct solution anyway, since it fails to take wide characters (see `g_unichar_iswide()`) into account.

Note also that there are various `printf()` parameters which are platform dependent. GLib provides platform independent macros for these parameters which should be used instead. A common example is `G_GUINT64_FORMAT`, which should be used instead of `%llu` or similar parameters for formatting 64-bit integers. These macros are all named `G_*_FORMAT`; see Basic Types.

## General String Manipulation

- `g_strdup()`
- `g_strndup()`
- `g_strdupv()`
- `g_strnfill()`
- `g_stpcpy()`
- `g_strstr_len()`
- `g_strrstr()`
- `g_strrstr_len()`
- `g_str_has_prefix()`
- `g_str_has_suffix()`
- `g_strcmp0()`
- `g_str_to_ascii()`
- `g_str_tokenize_and_fold()`
- `g_str_match_string()`

For users of GLib in C, the `g_set_str()` inline function also exists to set a string and handle copying the new value and freeing the old one.

## String Copying

- `g_strlcpy()`
- `g_strlcat()`

## Printing

- `g_strdup_printf()`
- `g_strdup_vprintf()`
- `g_printf()`
- `g_vprintf()`
- `g_fprintf()`
- `g_vfprintf()`
- `g_sprintf()`
- `g_vsprintf()`
- `g_snprintf()`
- `g_vsnprintf()`
- `g_vasprintf()`
- `g_printf_string_upper_bound()`

## ASCII

- `g_str_is_ascii()`
- `g_ascii_isalnum()`
- `g_ascii_isalpha()`
- `g_ascii_iscntrl()`
- `g_ascii_isdigit()`
- `g_ascii_isgraph()`
- `g_ascii_islower()`
- `g_ascii_isprint()`
- `g_ascii_ispunct()`
- `g_ascii_isspace()`
- `g_ascii_isupper()`
- `g_ascii_isxdigit()`

## ASCII Parsing

- `g_ascii_digit_value()`
- `g_ascii_xdigit_value()`

## ASCII Comparisons

- `g_ascii_strcasecmp()`
- `g_ascii_strncasecmp()`

## ASCII Case Manipulation

- `g_ascii_strup()`
- `g_ascii_strdown()`
- `g_ascii_tolower()`
- [func@GLib.ascii_toupper

## ASCII String Manipulation

- `g_strreverse()`

## ASCII Number Manipulation

- `g_ascii_strtoll()`
- `g_ascii_strtoull()`
- `G_ASCII_DTOSTR_BUF_SIZE`
- `g_ascii_strtod()`
- `g_ascii_dtostr()`
- `g_ascii_formatd()`
- `g_strtod()`

## ASCII Number Parsing

- `GNumberParserError`
- `g_ascii_string_to_signed()`
- `g_ascii_string_to_unsigned()`

## Whitespace Removal

- `g_strchug()`
- `g_strchomp()`
- `g_strstrip()`

## Find and Replace

- `g_strdelimit()`
- `G_STR_DELIMITERS`
- `g_strescape()`
- `g_strcompress()`
- `g_strcanon()`

## Splitting and Joining

- `g_strsplit()`
- `g_strsplit_set()`
- `g_strconcat()`
- `g_strjoin()`
- `g_strjoinv()`

## String Arrays

- `GStrv`
- `g_strfreev()`
- `g_strv_length()`
- `g_strv_contains()`
- `g_strv_equal()`

## String Array Builder

- `GStrvBuilder`
- `g_strv_builder_new()`
- `g_strv_builder_ref()`
- `g_strv_builder_unref()`
- `g_strv_builder_add()`
- `g_strv_builder_addv()`
- `g_strv_builder_add_many()`
- `g_strv_builder_take()`
- `g_strv_builder_end()`

## POSIX Errors

- `g_strerror()`
- `g_strsignal()`

## Deprecated API

- `g_strup()`
- `g_strdown()`
- `g_strcasecmp()`
- `g_strncasecmp()`