油嘴串效用函数(echivalent为strcspn)
问题描述:
我想提取的“/”油嘴串效用函数(echivalent为strcspn)
例如第一次出现之后的存在的字符串一个具有一个char *卡在这种形式中的一种:
hw:0/Line
default/Master
,我需要的char *通道(线路,硕士,...)
在标准C可以用 “strcspn” 和 “strlen的”
char *card, *channel;
int i;
i = strcspn(card, "/");
if (i == strlen(card)) {
channel = "Master";
} else {
card[i] = 0;
channel = card + i + 1;
}
如何使用glib字符串实用程序函数执行此操作?
答
strcspn
和strlen
怎么了?
GLib重新实现了很多东西C99和POSIX,因为它的目标是C89编译器。它不会重新实现像strlen
或strcspn
之类的东西,因为不需要它们在C89中。
GLib也倾向于实现很多功能的UTF-8版本,虽然有一个g_utf8_strlen
我不知道g_utf8_strcspn
我不认为这是真的需要在这种情况下。如果是,那么你可能必须通过迭代遍历字符串g_utf8_next_char
并检查g_utf8_get_char
或g_utf8_get_char_validated
的结果来自己实现它。