我正在尝试解析以下字符串:
"USBSTOR\DISK&VEN_JETFLASH&PROD_TRANSCEND_8GB&REV_1100\00H8096XQ9UW1BQ5&0:JetFlash Transcend 8GB USB Device"
基于'\'
(字符)
问题 1:但是这个字符被认为是转义字符
问题 2:字符串中间的 \0
被认为是字符串的结尾。
我尝试了很多方法。
(i) 我尝试将 '\'
替换为另一个字符,例如 '$'
并尝试使用 sscanf()
进行解析,但是它没有用。
你们能推荐点什么吗?
#include <string.h>
#include <stdio.h>
int main()
{
char str[80] = "This is \www.tutorialspoint.com \website";
const char s[2] = "\\";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );
token = strtok(NULL, s);
}
return(0);
}
最佳答案
做这个修改
char str[80] = "这是\\www.tutorialspoint.com\\website";
这样,您的输出是:
This is
www.tutorialspoint.com
website
记住:您在代码中使用的任何字符串文字都需要反斜杠的转义序列。
关于c++ - 基于转义字符的字符串解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373940/