10

strncmp を使用してみましたが、抽出したい特定のバイト数を指定した場合にのみ機能します。

char line[256] = This "is" an example. //I want to extract "is"
char line[256] = This is "also" an example. // I want to extract "also"
char line[256] = This is the final "example".  // I want to extract "example"
char substring[256]

"" の間のすべての要素を抽出するにはどうすればよいですか? 変数部分文字列に入れますか?

4

5 に答える 5

2

ライブラリのサポートなしでやりたい場合...

void extract_between_quotes(char* s, char* dest)
{
   int in_quotes = 0;
   *dest = 0;
   while(*s != 0)
   {
      if(in_quotes)
      {
         if(*s == '"') return;
         dest[0]=*s;
         dest[1]=0;
         dest++;
      }
      else if(*s == '"') in_quotes=1;
      s++;
   }
}

それからそれを呼び出します

extract_between_quotes(line, substring);

于 2013-10-24T01:58:37.600 に答える
0
#include <string.h>
...        
substring[0] = '\0';
const char *start = strchr(line, '"') + 1;
strncat(substring, start, strcspn(start, "\""));

境界とエラー チェックは省略されています。strtok副作用があるので避けましょう。

于 2013-10-24T12:30:17.047 に答える