Search code examples
cglib

Replace text with GLib


I have a string with a ton of \n's, I'd like to turn each of them into \001\n\001CW1 . What's the easiest way to do this, using glib? No regex, just plain replace.


Solution

  • This can easily be done with g_strsplit and g_strjoinv:

    char **split = g_strsplit(text, "\n", -1);
    g_free(text);
    text = g_strjoinv("\001\n\001CW1 ", split);
    g_strfreev(split);
    

    While this is a very common construct in scripting languages, it's a bit weird to do in C/with glib, but I guess it is the right/only way.