我有以下字符串
/foo123/bar123/card45/foofoo/1.3/
我想解析单词“card”后面的数字,在上面的示例中为 45。我应该为此使用 sscanf 吗?如果是,我将如何做?
谢谢
最佳答案
Should I use
sscanf
for [XYZ problem]
没有。
但是你可以使用 strstr
和 strtol
代替:
const char *s = "/foo123/bar123/card45/foofoo/1.3/";
const char *p = "card";
const char *t = strstr(s, p);
int i = -1; // a negative number indicates a parse failure, for example
if (t != NULL) {
t += strlen(p);
char *end;
i = strtol(t, &end, 10);
if (!end || *end != '/') {
// parsing the number failed
}
}
关于c - 使用 C sscanf 解析目录路径中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306314/