【发布时间】:2010-04-25 15:19:18
【问题描述】:
我正在尝试以一种好的方式解析下面的字符串,以便获得子字符串stringI-wantToGet:
const char *str = "Hello \"FOO stringI-wantToGet BAR some other extra text";
str 的长度会有所不同,但总是相同的模式 - FOO 和 BAR
我的想法是这样的:
const char *str = "Hello \"FOO stringI-wantToGet BAR some other extra text";
char *probe, *pointer;
probe = str;
while(probe != '\n'){
if(probe = strstr(probe, "\"FOO")!=NULL) probe++;
else probe = "";
// Nulterm part
if(pointer = strchr(probe, ' ')!=NULL) pointer = '\0';
// not sure here, I was planning to separate it with \0's
}
任何帮助将不胜感激。
【问题讨论】:
-
为什么 str 不包含 '\n'? C 字符串以 NULL 结尾,因此您可能应该探测 0(或 NULL)。
-
首先,告诉我们运行这段代码时会发生什么。
-
感谢大家的建议!很棒的信息!