在字符串后得到第一个单词

时间:2019-01-21 11:18:54

标签: c

我正在使用esp8266,我需要先在单词“ + CWLAP:”之后加上iside引号。

这是我的输出结果。

AT+CWLAP
+CWLAP:(3,"MainNetwork",-76,"00:15:6d:a6:61:fa",1,107,0,5,3,3,0)
+CWLAP:(1,"SubNetwork",-79,"00:4f:62:19:19:43",9,85,0,0,0,3,0)

我需要打印MainNetwork和SubNetwork。 这就是我想要做的

char *p =strtok(Receive.bytes, "+CWLAP:"); 

p = strtok(NULL,  "\"");
p = strtok(NULL,  "\"");
printf("%s\r\n",p);
__delay_ms(20);

p = strtok(NULL,  "+CWLAP:");
p = strtok(NULL,  "\"");
p = strtok(NULL,  "\"");
printf("%s\r\n",p);
__delay_ms(20);

有些共鸣,我在输出中考虑了这一点

MainNetwork
,1,107,0,5,3,3,0)
+CWLAP:(1,

2 个答案:

答案 0 :(得分:1)

替换

char *p =strtok(Receive.bytes, "+CWLAP:");
p = strtok(NULL,  "\"");

作者

char *p =strstr(Receive.bytes, "+CWLAP:"); 
p = strtok(p,  "\"");

因为在您的第一个strtok“ + CWLAP:”中是一串分隔符,所以 strtok 将给您 AT 放在'+'之前

与第二个相似,但 strtok 添加空字符,因此第二个 strstr 必须在任何 strtok 之前完成。

完整的解决方案可以是:

  char * p1 = strstr(Receive.bytes, "+CWLAP:"); 
  char * p3 = strstr(p1 + 7, "+CWLAP:"); 
  char * p2;

  p2 = strtok(p1,  "\"");
  p2 = strtok(NULL,  "\"");
  printf("%s\r\n",p2);

  p2 = strtok(p3,  "\"");
  p2 = strtok(NULL,  "\"");
  printf("%s\r\n",p2);

附加说明, strtok 修改Receive.bytes,添加空字符,可能是您需要首先复制字符串( strdup )吗?

执行:

MainNetwork
SubNetwork

并管理任意数量的行:

char * p1 = strstr(Receive.bytes, "+CWLAP:");

while (p1 != NULL) {
  p1 += 7; /* bypass +CWLAP: */

  char * p2 = strstr(p1, "+CWLAP:");
  char * s = strtok(strchr(p1, '"') + 1,  "\"");

  printf("%s\r\n", s);

  p1 = p2;
}

我用 strchr 替换了第一个 strtok ,因为我们不需要中间字符串

答案 1 :(得分:0)

由于strtok修改了源数组,因此strcspn可用于与strstr一起遍历数组。
搜索可以包装在循环中以查找所有匹配项。
strstr将在源数组中找到子数组的完全匹配。
从匹配开始,strcspn会将字符计数为与分隔符字符串中的任何字符匹配的源中的下一个字符。此处仅将引号用作定界符。如果strcspn未找到匹配项,则将其计为终止零。
这会打印匹配的字符串,但是可以使用strncpy复制该字符串。注意终止零。

#include <stdio.h>
#include <string.h>

int main()
{
    char bytes[] = "AT+CWLAP\n+CWLAP:(3,\"MainNetwork\",-76,\"00:15:6d:a6:61:fa\",1,107,0,5,3,3,0)\n+CWLAP:(1,\"SubNetwork\",-79,\"00:4f:62:19:19:43\",9,85,0,0,0,3,0)";
    char *quote = "\"";
    char *cwlap = "+CWLAP:";
    char *match = NULL;
    char *openquote = NULL;
    char *closequote = NULL;
    size_t skip = 0;
    size_t span = 0;

    closequote = bytes;
    while ( closequote && *closequote) {//not NULL and not zero terminator
        if ( ( match = strstr ( closequote, cwlap))) {//find exact sub-string match
            skip = strcspn ( match, quote);//count characters to quote
            openquote = match + skip;//point to quote
            if ( *openquote) {//not at terminating zero
                openquote++;//point to character after quote
                span = strcspn ( openquote, quote);//count characters to quote
                closequote = openquote + span;//point to quote
                if ( *closequote) {//not at terminating zero
                    printf ( "%.*s\n", (int)span, openquote);//print span number of characters
                    closequote++;//point to character after quote
                }
            }
            else {
                closequote = NULL;
            }
        }
        else {
            closequote = NULL;
        }
    }
    return 0;
}