split
Description:
public string[] split (string delimiter, int max_tokens = 0)
Splits a string into a maximum of max_tokens
pieces, using the given delimiter
.
If max_tokens
is reached, the remainder of string
is appended to the last token.
As an example, the result of `g_strsplit ("gchar:a
gchar:bc
gchar::d
:", ":", -1)` is an array containing
the six strings "", "a", "bc", "", "d" 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()`.
Example: Split a string:
public static int main (string[] args) {
string doctors_str = "William Hartnell, Patrick Troughton, Jon Pertwee, Tom Baker, Peter Davison, Colin Baker, Sylvester McCoy";
// Output:
// ``'William Hartnell'``
// ``'Patrick Troughton'``
// ``'Jon Pertwee'``
// ``'Tom Baker'``
// ``'Peter Davison'``
// ``'Colin Baker'``
// ``'Sylvester McCoy'``
string[] doctors = doctors_str.split (", ");
foreach (unowned string str in doctors) {
print ("'%s'\n", str);
}
// Output:
// ``'Alistair'``
// ``'Gordon Lethbridge-Stewart'``
string line = "Alistair Gordon Lethbridge-Stewart";
string[] lines = line.split (" ", 2);
foreach (unowned string str in lines) {
print ("'%s'\n", str);
}
return 0;
}
valac --pkg glib-2.0 string.split.vala
Parameters:
delimiter |
a string which specifies the places at which to split the string. The delimiter is not included in any of the resulting strings, unless
|
max_tokens |
the maximum number of pieces to split |
string |
a string to split |
Returns:
a newly-allocated array of strings, freed with [func@GLib.strfreev] |