index_of_nth_char
Description:
Converst utf8-char-position to a byte offset.
Since 2.10, this function allows to pass a negative offset to step backwards. It is usually worth stepping backwards from the end instead of forwards if offset is in the last fourth of the string, since moving forward is about 3 times faster than moving backward.
Example: Convert a character index to a byte index:
static int main (string[] args) {
// Output:
// ``symbol: 0 byte-offset: 0: 楽``
// ``symbol: 1 byte-offset: 3: あ``
// ``symbol: 2 byte-offset: 6: れ``
// ``symbol: 3 byte-offset: 9: ば``
// ``symbol: 4 byte-offset: 12: 苦``
// ``symbol: 5 byte-offset: 15: あ``
// ``symbol: 6 byte-offset: 18: り``
// ``symbol: 7 byte-offset: 21: 。``
string wisdom = "楽あれば苦あり。";
for (int i = 0; i < 8; i++) {
int bpos = wisdom.index_of_nth_char (i);
print ("symbol: %d\tbyte-offset: %d:\t%s\n", i, bpos, wisdom.get_char (bpos).to_string ());
}
return 0;
}
valac --pkg glib-2.0 string.index_of_nth_char.vala
Parameters:
c |
the utf8-character offset |
Returns:
the byte offset of the nth utf8-character. |