Snippet. Vala. Convert String to Array of Characters By Casting

This little snippet demonstrates how to convert a string into array of characters (chars) in Vala. It also demonstrates the backwards conversion from array of characters (chars) back to string.

// Convert Vala string to array of chars
string my_string = "Hello, world!";
char* my_chars = (char*) my_string;

stdout.printf("%c\n", my_chars[7]); // Prints w

// Convert Vala array of chars to string
string new_string = (string)my_chars;
stdout.printf(new_string+ "\n");

Updated on: 23 Nov 2024