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

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

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



ASCII Parsing

ASCII Comparisons

ASCII Case Manipulation

ASCII String Manipulation

ASCII Number Manipulation

ASCII Number Parsing

Whitespace Removal

Find and Replace

Splitting and Joining

String Arrays

String Array Builder

POSIX Errors

Deprecated API