How to get a list of unichars from a string?

I sometimes need to work on strings unichar by unichar in an array. At the moment I get the list of unichars like this:

Gee.ArrayList<unichar> unichars_for_string(string s) {
    var chars = new Gee.ArrayList<unichar>();
    unichar c = 0;
    int index = 0;
    for (int i = 0; s.get_next_char(ref index, out c); i++)
    return chars;

Is there a better way?

PS the chars.add(c); has the correct indentation but it doesn’t seem to show up.

1 Like

What are you trying to improve exactly?

Personally I’d use a while loop instead and probably a simple array unless I really needed something that Gee.ArrayList provides.

But that’s not going to make any significant difference.

I wanted to compare two strings unichar by unichar. In the end I did this:

// a and b are strings; size is min(a.char_count(), b.char_count())
for (int i = 0; i < size; i++) {
    int j = a.index_of_nth_char(i);
    int k = b.index_of_nth_char(i);
    if (a.get_char(j) == b.get_char(k))
	on_equal(); // action to take

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.