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
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
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
ASCII Comparisons
ASCII Case Manipulation
ASCII String Manipulation
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
Whitespace Removal
Find and Replace
Splitting and Joining
String Arrays
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()