1 <!-- ##### SECTION Title ##### -->
2 String Utility Functions
4 <!-- ##### SECTION Short_Description ##### -->
5 various string-related functions.
7 <!-- ##### SECTION Long_Description ##### -->
9 This section describes a number of utility functions for creating,
10 duplicating, and manipulating strings.
13 <!-- ##### SECTION See_Also ##### -->
18 <!-- ##### FUNCTION g_strdup ##### -->
21 The returned string should be freed when no longer needed.
24 @str: the string to duplicate.
25 @Returns: a newly-allocated copy of @str.
28 <!-- ##### FUNCTION g_strndup ##### -->
30 Duplicates the first @n characters of a string, returning a newly-allocated
31 buffer @n + 1 characters long which will always be null-terminated.
32 If @str is less than @n characters long the buffer is padded with nulls.
33 The returned value should be freed when no longer needed.
36 @str: the string to duplicate part of.
37 @n: the maximum number of characters to copy from @str.
38 @Returns: a newly-allocated buffer containing the first @n characters of @str,
42 <!-- ##### FUNCTION g_strnfill ##### -->
44 Creates a new string @length characters long filled with @fill_char.
45 The returned string should be freed when no longer needed.
48 @length: the length of the new string.
49 @fill_char: the character to fill the string with.
50 @Returns: a newly-allocated string filled the @fill_char.
53 <!-- ##### FUNCTION g_strlcpy ##### -->
64 <!-- ##### FUNCTION g_strlcat ##### -->
75 <!-- ##### FUNCTION g_strdup_printf ##### -->
77 Similar to the standard C <function>sprintf()</function> function
78 but safer, since it calculates the maximum space required and allocates
79 memory to hold the result.
80 The returned string should be freed when no longer needed.
83 @format: the standard <function>sprintf()</function> format string.
84 @Varargs: the parameters to insert into the format string.
85 @Returns: a newly-allocated string holding the result.
88 <!-- ##### FUNCTION g_strdup_vprintf ##### -->
90 Similar to the standard C <function>vsprintf()</function> function
91 but safer, since it calculates the maximum space required and allocates
92 memory to hold the result.
93 The returned string should be freed when no longer needed.
96 @format: the standard <function>sprintf()</function> format string.
97 @args: the list of parameters to insert into the format string.
98 @Returns: a newly-allocated string holding the result.
101 <!-- ##### FUNCTION g_snprintf ##### -->
103 A safer form of the standard <function>sprintf()</function> function.
104 The output is guaranteed to not exceed @n characters (including the
105 terminating NULL character), so it is easy to ensure that a buffer overflow
109 See also g_strdup_printf().
113 In versions of GLib prior to 1.2.3, this function may return -1 if the output
114 was truncated, and the truncated string may not be NULL-terminated.
118 @string: the buffer to hold the output.
119 @n: the maximum number of characters to produce (including the terminating null
121 @format: the format string. See the <function>sprintf()</function>
123 @Varargs: the arguments to insert in the output.
124 @Returns: the length of the output string.
127 <!-- ##### FUNCTION g_vsnprintf ##### -->
129 A safer form of the standard <function>vsprintf()</function> function.
130 The output is guaranteed to not exceed @n characters (including the
131 terminating NULL character), so it is easy to ensure that a buffer overflow
135 See also g_strdup_vprintf().
139 In versions of GLib prior to 1.2.3, this function may return -1 if the output
140 was truncated, and the truncated string may not be NULL-terminated.
144 @string: the buffer to hold the output.
145 @n: the maximum number of characters to produce (including the terminating null
147 @format: the format string. See the <function>sprintf()</function>
149 @args: the list of arguments to insert in the output.
150 @Returns: the length of the output string.
153 <!-- ##### FUNCTION g_printf_string_upper_bound ##### -->
155 Calculates the maximum space needed to store the output of the
156 <function>sprintf()</function> function.
159 @format: the format string. See the <function>printf()</function>
161 @args: the parameters to be inserted into the format string.
162 @Returns: the maximum space needed to store the formatted string.
165 <!-- ##### FUNCTION g_strup ##### -->
167 Converts a string to upper case.
170 @string: the string to convert.
174 <!-- ##### FUNCTION g_strdown ##### -->
176 Converts a string to lower case.
179 @string: the string to convert.
183 <!-- ##### FUNCTION g_strcasecmp ##### -->
185 A case-insensitive string comparison, corresponding to the standard
186 <function>strcasecmp()</function> function on platforms which support it.
190 @s2: a string to compare with @s1.
191 @Returns: 0 if the strings match, a negative value if @s1 < @s2, or a positive
195 <!-- ##### FUNCTION g_strncasecmp ##### -->
197 A case-insensitive string comparison, corresponding to the standard
198 <function>strncasecmp()</function> function on platforms which support it.
199 It is similar to g_strcasecmp() except it only compares the first @n characters
204 @s2: a string to compare with @s1.
205 @n: the maximum number of characters to compare.
206 @Returns: 0 if the strings match, a negative value if @s1 < @s2, or a positive
210 <!-- ##### FUNCTION g_strreverse ##### -->
212 Reverses all of the characters in a string.
213 For example, g_strreverse ("abcdef") would be "fedcba".
216 @string: the string to reverse.
220 <!-- ##### FUNCTION g_strtod ##### -->
222 Converts a string to a gdouble value.
223 It calls the standard <function>strtod()</function> function
224 to handle the conversion, but if the string is not completely converted
225 it attempts the conversion again in the "C" locale, and returns the best
229 @nptr: the string to convert to a numeric value.
230 @endptr: if non-NULL, it returns the character after the last character used
232 @Returns: the gdouble value.
235 <!-- ##### FUNCTION g_strchug ##### -->
237 Removes leading whitespace from a string, by moving the rest of the
241 @string: a string to remove the leading whitespace from.
245 <!-- ##### FUNCTION g_strchomp ##### -->
247 Removes trailing whitespace from a string.
250 @string: a string to remove the trailing whitespace from.
254 <!-- ##### MACRO g_strstrip ##### -->
256 Removes leading and trailing whitespace from a string.
259 @string: a string to remove the leading and trailing whitespace from.
262 <!-- ##### FUNCTION g_strdelimit ##### -->
264 Converts any delimiter characters in @string to @new_delimiter.
265 Any characters in @string which are found in @delimiters are changed
266 to the @new_delimiter character.
269 @string: the string to convert.
270 @delimiters: a string containing the current delimiters, or NULL to use the
271 standard delimiters defined in #G_STR_DELIMITERS.
272 @new_delimiter: the new delimiter character.
276 <!-- ##### MACRO G_STR_DELIMITERS ##### -->
278 The standard delimiters, used in #g_strdelimit.
283 <!-- ##### FUNCTION g_strescape ##### -->
285 Escapes all backslash characters, '\' in a string, by inserting a second '\'.
290 @Returns: a newly allocated copy of @string, with all backslash characters
291 escaped using a second backslash.
292 <!-- # Unused Parameters # -->
293 @string: a string to escape the backslashes in.
296 <!-- ##### FUNCTION g_strcompress ##### -->
305 <!-- ##### FUNCTION g_strcanon ##### -->
316 <!-- ##### FUNCTION g_strsplit ##### -->
318 Splits a string into a maximum of @max_tokens pieces, using the given
319 @delimiter. If @max_tokens is reached, the final string in the returned
320 string array contains the remainder of @string.
323 @string: a string to split.
324 @delimiter: a string which specifies the places at which to split the string.
325 The delimiter is not included in any of the resulting strings, unless
326 max_tokens is reached.
327 @max_tokens: the maximum number of strings to split @string into. If this is
328 less than 1, the string is split completely.
329 @Returns: a newly-allocated array of strings. Use g_strfreev() to free it.
332 <!-- ##### FUNCTION g_strfreev ##### -->
334 Frees a NULL-terminated array of strings, and the array itself.
337 @str_array: a NULL-terminated array of strings to free.
340 <!-- ##### FUNCTION g_strconcat ##### -->
342 Concatenates all of the given strings into one long string.
343 The returned string should be freed when no longer needed.
346 @string1: The first string to add, which must not be NULL.
347 @Varargs: a NULL-terminated list of strings to append to the string.
348 @Returns: a newly-allocated string containing all the string arguments.
351 <!-- ##### FUNCTION g_strjoin ##### -->
353 Joins a number of strings together to form one long string, with the optional
354 @separator inserted between each of them.
357 @separator: a string to insert between each of the strings, or NULL.
358 @Varargs: a NULL-terminated list of strings to join.
359 @Returns: a newly-allocated string containing all of the strings joined
360 together, with @separator between them.
363 <!-- ##### FUNCTION g_strjoinv ##### -->
365 Joins a number of strings together to form one long string, with the optional
366 @separator inserted between each of them.
369 @separator: a string to insert between each of the strings, or NULL.
370 @str_array: a NULL-terminated array of strings to join.
371 @Returns: a newly-allocated string containing all of the strings joined
372 together, with @separator between them.
375 <!-- ##### FUNCTION g_strerror ##### -->
377 Returns a string corresponding to the given error code, e.g. "no such process".
378 This function is included since not all platforms support the
379 <function>strerror()</function> function.
382 @errnum: the system error number. See the standard C %errno
384 @Returns: a string describing the error code.
385 If the error code is unknown, it returns "unknown error (<code>)".
386 The string can only be used until the next call to g_strerror.
389 <!-- ##### FUNCTION g_strsignal ##### -->
391 Returns a string describing the given signal, e.g. "Segmentation fault".
392 This function is included since not all platforms support the
393 <function>strsignal()</function> function.
396 @signum: the signal number. See the <literal>signal</literal>
398 @Returns: a string describing the signal.
399 If the signal is unknown, it returns "unknown signal (<signum>)".
400 The string can only be used until the next call to g_strsignal.