substring
Description:
Returns the characters in a string beginning at the specified location through the specified number of characters.
offset
is a byte index. The index of the first character is 0, and the index of the last character is 1 less than the length of
the string. string.substring begins extracting characters at offset
and collects len
characters (unless it reaches the end of the string first, in which case it will return fewer).
If offset
is positive and is greater than or equal to the length of the string, string.substring
returns null and reports a critical warning.
If offset
is negative, string.substring uses it as a character index from the end of the string. If
start is negative and abs(start) is larger than the length of the string, string.substring,
string.substring returns null and reports a critical warning.
If len
is 0, string.substring returns an empty string.
If len
is omitted or negative, string.substring extracts characters to the end of the string.
Example: Substring:
static int main (string[] args) {
string wisdom = "水は方円の器に従い、人は善悪の友による。";
int start = wisdom.index_of_nth_char (5); // 器
int end = wisdom.index_of_nth_char (10); // 人
// Output: ``器に従い、``
string res = wisdom.substring (start, end - start);
print ("%s\n", res);
return 0;
}
valac --pkg glib-2.0 string.substring.vala
Parameters:
offset |
Location at which to begin extracting characters. (in bytes) |
len |
The number of characters to extract. (in bytes) |
Returns:
the specified substring. |