0

I want to extract string that exist after first occurrence of "/"

For example a have a char *card in one of this form:

    hw:0/Line

    default/Master

and I need char *channel (Line, Master, ...)

In standard C can use "strcspn" and "strlen"

    char *card, *channel;
    int i;

    i = strcspn(card, "/");
    if (i == strlen(card)) {
        channel = "Master";
    } else {
        card[i] = 0;
        channel = card + i + 1;
    }

How can I do this using glib string utility functions?

1 Answer 1

2

What's wrong with strcspn and strlen?

GLib reimplements a lot of stuff C99 and POSIX because it targets C89 compilers. It doesn't reimplement things like strlen or strcspn because there is no need to—they are in C89.

GLib also tends to implement UTF-8 versions of a lot of functions, and while there is a g_utf8_strlen I'm not aware of a g_utf8_strcspn I don't think it's really necessary in this case. If it is then you'll probably have to implement it yourself by iterating through the string with g_utf8_next_char and checking against the result of g_utf8_get_char or g_utf8_get_char_validated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.