[ Version ( since = "2.2" ) ]
[ CCode ( cname = "g_utf8_strreverse" ) ]
public string reverse (ssize_t len = -1)

Reverses a UTF-8 string.

str must be valid UTF-8 encoded text. (Use validate on all text before trying to use UTF-8 utility functions with it.)

This function is intended for programmatic uses of reversed strings. It pays no attention to decomposed characters, combining marks, byte order marks, directional indicators (LRM, LRO, etc) and similar characters which might need special handling when reversing a string for display purposes.

Note that unlike g_strreverse, this function returns newly-allocated memory, which should be freed with g_free when no longer needed.

Example: Reverse a string:

public static int main (string[] args) {
// Output: ``I ain't afraid of no dalek!``
string my_secret = "!kelad on fo diarfa t'nia I".reverse ();
print ("%s\n", my_secret);
return 0;

valac --pkg glib-2.0 string.reverse.vala



the maximum length of str to use, in bytes. If len < 0, then the string is nul-terminated.


a UTF-8 encoded string


a newly-allocated string which is the reverse of str