📄 string_utils.sgml
字号:
<!-- ##### SECTION Title ##### -->String Utility Functions<!-- ##### SECTION Short_Description ##### -->various string-related functions<!-- ##### SECTION Long_Description ##### --><para>This section describes a number of utility functions for creating,duplicating, and manipulating strings.</para><para>Note that the functions g_printf(), g_fprintf(), g_sprintf(), g_snprintf(),g_vprintf(), g_vfprintf(), g_vsprintf() and g_vsnprintf() are declared in the header <filename>gprintf.h</filename> which is <emphasis>not</emphasis>included in <filename>glib.h</filename> (otherwise using <filename>glib.h</filename> would drag in <filename>stdio.h</filename>), soyou'll have to explicitly include <literal><glib/gprintf.h></literal> in order to use the GLib printf() functions. </para><para id="string-precision">While you may use the printf() functions to format UTF-8 strings, notice thatthe precision of a <literal>%Ns</literal> parameter is interpreted as thenumber of <emphasis>bytes</emphasis>, not <emphasis>characters</emphasis> 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 <literal>%Ns</literal> parameterconsists of a whole number of characters in the current encoding. So, unless youare sure you are always going to be in an UTF-8 locale or your know your text is restricted to ASCII, avoid using <literal>%Ns</literal>.If your intention is to format strings for a certain number of columns, then <literal>%Ns</literal> is not a correct solution anyway, since it fails to take wide characters (see g_unichar_iswide()) into account.</para><!-- ##### SECTION See_Also ##### --><para></para><!-- ##### SECTION Stability_Level ##### --><!-- ##### FUNCTION g_strdup ##### --><para>Duplicates a string.If @str is %NULL it returns %NULL.The returned string should be freed when no longer needed.</para>@str: the string to duplicate.@Returns: a newly-allocated copy of @str.<!-- ##### FUNCTION g_strndup ##### --><para></para>@str: @n: @Returns: <!-- ##### FUNCTION g_strdupv ##### --><para></para>@str_array: @Returns: <!-- ##### FUNCTION g_strnfill ##### --><para></para>@length: @fill_char: @Returns: <!-- ##### FUNCTION g_stpcpy ##### --><para></para>@dest: @src: @Returns: <!-- ##### FUNCTION g_strstr_len ##### --><para></para>@haystack: @haystack_len: @needle: @Returns: <!-- ##### FUNCTION g_strrstr ##### --><para></para>@haystack: @needle: @Returns: <!-- ##### FUNCTION g_strrstr_len ##### --><para></para>@haystack: @haystack_len: @needle: @Returns: <!-- ##### FUNCTION g_str_has_prefix ##### --><para></para>@str: @prefix: @Returns: <!-- ##### FUNCTION g_str_has_suffix ##### --><para></para>@str: @suffix: @Returns: <!-- ##### FUNCTION g_strlcpy ##### --><para>Portability wrapper that calls strlcpy() on systems which have it, and emulatesstrlcpy() otherwise. Copies @src to @dest; @dest is guaranteed to benul-terminated; @src must be nul-terminated; @dest_size is the buffer size, notthe number of chars to copy. Caveat: strlcpy() is supposedly more secure thanstrcpy() or strncpy(), but if you really want to avoid screwups, g_strdup() isan even better idea.</para>@dest: destination buffer@src: source buffer@dest_size: length of @dest in bytes@Returns: length of @src<!-- ##### FUNCTION g_strlcat ##### --><para>Portability wrapper that calls strlcat() on systems which have it, and emulates it otherwise. Appends nul-terminated @src string to @dest, guaranteeingnul-termination for @dest. The total size of @dest won't exceed@dest_size. Caveat: this is supposedly a more secure alternative to strcat() orstrncat(), but for real security g_strconcat() is harder to mess up.</para>@dest: destination buffer, already containing one nul-terminated string@src: source buffer@dest_size: length of @dest buffer in bytes (not length of existing string inside @dest)@Returns: length of @src plus initial length of string in @dest<!-- ##### FUNCTION g_strdup_printf ##### --><para>Similar to the standard C sprintf() functionbut safer, since it calculates the maximum space required and allocatesmemory to hold the result.The returned string should be freed when no longer needed.</para>@format: a standard printf() format string, but notice <link linkend="string-precision">string precision pitfalls</link>.@Varargs: the parameters to insert into the format string.@Returns: a newly-allocated string holding the result.<!-- ##### FUNCTION g_strdup_vprintf ##### --><para>Similar to the standard C vsprintf() functionbut safer, since it calculates the maximum space required and allocatesmemory to hold the result.The returned string should be freed when no longer needed.</para><para>See also g_vasprintf(), which offers the same functionality, but additionallyreturns the length of the allocated string. </para>@format: a standard printf() format string, but notice <link linkend="string-precision">string precision pitfalls</link>.@args: the list of parameters to insert into the format string.@Returns: a newly-allocated string holding the result.<!-- ##### FUNCTION g_printf ##### --><para></para>@format: @Varargs: @Returns: <!-- ##### FUNCTION g_vprintf ##### --><para></para>@format: @args: @Returns: <!-- ##### FUNCTION g_fprintf ##### --><para></para>@file: @format: @Varargs: @Returns: <!-- ##### FUNCTION g_vfprintf ##### --><para></para>@file: @format: @args: @Returns: <!-- ##### FUNCTION g_sprintf ##### --><para></para>@string: @format: @Varargs: @Returns: <!-- ##### FUNCTION g_vsprintf ##### --><para></para>@string: @format: @args: @Returns: <!-- ##### FUNCTION g_snprintf ##### --><para></para>@string: @n: @format: @Varargs: @Returns: <!-- ##### FUNCTION g_vsnprintf ##### --><para></para>@string: @n: @format: @args: @Returns: <!-- ##### FUNCTION g_vasprintf ##### --><para></para>@string: @format: @args: @Returns: <!-- ##### FUNCTION g_printf_string_upper_bound ##### --><para>Calculates the maximum space needed to store the output of the sprintf() function.</para>@format: the format string. See the printf() documentation.@args: the parameters to be inserted into the format string.@Returns: the maximum space needed to store the formatted string.<!-- ##### FUNCTION g_ascii_isalnum ##### --><para>Determines whether a character is alphanumeric.</para><para>Unlike the standard C library isalnum() function, this onlyrecognizes standard ASCII letters and ignores the locale, returning%FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII alphanumeric character<!-- ##### FUNCTION g_ascii_isalpha ##### --><para>Determines whether a character is alphabetic (i.e. a letter).</para><para>Unlike the standard C library isalpha() function, this onlyrecognizes standard ASCII letters and ignores the locale, returning%FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII alphabetic character<!-- ##### FUNCTION g_ascii_iscntrl ##### --><para>Determines whether a character is a control character.</para><para>Unlike the standard C library iscntrl() function, this onlyrecognizes standard ASCII control characters and ignores the locale,returning %FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII control character.<!-- ##### FUNCTION g_ascii_isdigit ##### --><para>Determines whether a character is digit (0-9).</para><para>Unlike the standard C library isdigit() function,this takes a <type>char</type>, not an <type>int</type>, so don't call iton %EOF but no need to cast to #guchar before passing a possiblynon-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII digit.<!-- ##### FUNCTION g_ascii_isgraph ##### --><para>Determines whether a character is a printing character and not a space.</para><para>Unlike the standard C library isgraph() function, this only recognizes standard ASCII characters and ignores the locale, returning %FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII printing character other than space.<!-- ##### FUNCTION g_ascii_islower ##### --><para>Determines whether a character is an ASCII lower case letter.</para><para>Unlike the standard C library islower() function, this only recognizes standard ASCII letters and ignores the locale,returning %FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to worry about casting to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII lower case letter<!-- ##### FUNCTION g_ascii_isprint ##### --><para>Determines whether a character is a printing character.</para><para>Unlike the standard C library isprint() function, this only recognizes standard ASCII characters and ignores the locale, returning %FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII printing character.<!-- ##### FUNCTION g_ascii_ispunct ##### --><para>Determines whether a character is a punctuation character.</para><para>Unlike the standard C library ispunct() function, this only recognizes standard ASCII letters and ignores the locale, returning %FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII punctuation character.<!-- ##### FUNCTION g_ascii_isspace ##### --><para>Determines whether a character is a white-space character.</para><para>Unlike the standard C library isspace() function, this only recognizes standard ASCII white-space and ignores the locale, returning %FALSE for all non-ASCII characters. Also unlike the standardlibrary function, this takes a <type>char</type>, not an <type>int</type>, so don't call it on %EOF but no need to cast to #guchar before passing a possibly non-ASCII character in.</para>@c: any character@Returns: %TRUE if @c is an ASCII white-space character<!-- ##### FUNCTION g_ascii_isupper ##### --><para>Determines whether a character is an ASCII upper case letter.</para><para>Unlike the standard C library isupper() function, this only recognizes standard ASCII letters and ignores the locale,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -