split_set
Description:
[ CCode ( array_length = false , array_null_terminated = true , cname = "g_strsplit_set" ) ]
public string[] split_set (string delimiters, int max_tokens = 0)
Splits string
into a number of tokens not containing any of the characters in delimiters
.
A token is the (possibly empty) longest string that does not contain any of the characters in delimiters
. If max_tokens
is reached, the remainder is appended to the last token.
For example, the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is an array containing the three strings "abc", "def", and "ghi".
The result of g_strsplit_set ("gchar:def
/ghi:", ":/", -1) is an array containing the four strings "", "def", "ghi", and "".
As a special case, the result of splitting the empty string "" is an empty array, not an array containing a single string. The reason for this special case is that being able to represent an empty array is typically more useful than consistent handling of empty elements. If you do need to represent empty elements, you'll need to check for the empty string before calling `g_strsplit_set()`.
Note that this function works on bytes not characters, so it can't be used to delimit UTF-8 strings for anything but ASCII characters.
Example: Split a string, multiple delimiters:
public static int main (string[] args) {
string names_str = "Ernst,Mach;Erwin,Schrödinger";
// Output:
// ``'Ernst'``
// ``'Mach'``
// ``'Erwin'``
// ``'Schrödinger'``
string[] names = names_str.split_set (",;");
foreach (unowned string str in names) {
print ("'%s'\n", str);
}
// Output:
// ``'Ernst'``
// ``'Mach;Erwin,Schrödinger'``
names = names_str.split_set (",;", 2);
foreach (unowned string str in names) {
print ("'%s'\n", str);
}
return 0;
}
valac --pkg glib-2.0 string.split_set.vala
Parameters:
delimiters |
a string containing characters that are used to split the string. Can be empty, which will result in no string splitting |
max_tokens |
the maximum number of tokens to split |
string |
a string to split |
Returns:
a newly-allocated array of strings. Use [func@GLib.strfreev] to free it. |