Re: [Vala] string manipulation question



On 05/02/2010 01:42 AM, Frederik wrote:
Am 01.05.2010 23:15, Charles Hixson wrote:
Is there a better way to do this?
You should use a 'join' method to make the intentions of your code clear and
to prepare for code reuse:

string htmlCardItemSeparated(string itemName, ArrayList<string>  itemList,
                              string separator = ", ")
{
        return itemList.is_empty ? "" :
               @"<tr><td>$itemName</td><td>$(join(itemList, separator))</td></tr>\n";
}


A 'join' method is most efficiently implemented with a StringBuilder:

string join(Iterable<string>  strings, string separator = ", ") {
        var builder = new StringBuilder();
        var it = strings.iterator();
        for (var has_next = it.first(); has_next; has_next = it.next()) {
                builder.append(it.get());
                if (it.has_next()) {
                        builder.append(separator);
                }
        }
        return builder.str;
}


Alternatively you could use 'string.joinv', but then you would have to convert
your list into a null-terminated array first.


Best regards,

Frederik
_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list
Thanks.  I'll have to look for documentation on StringBuilder.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]