#C/C++# 实现提取一段不规则字符串中两个双引号(其他符号)之间的内容

C 语言实现:

int main()
{
     
      char source[100] ={
     "\how are you = "nihao\""}char dest[100] = {
     0}int i,j,flag;
      while(source != '\0')
      {
     
         if(flag ==0 && source[i] == '\"')
         {
     
                       flag = 1}
         else if(flag == 1 && source[i] == '\"')
         {
     
                       dest[i] == '\0'//printf("%s\n",dest);
         }
         else if(flag == 1 && source[i] != '\"')
         {
     
                        dest[j++] = source[i]}
         i++}
     printf("dest = %s\n",dest)return 0}
//跳过第一个引号以前的字符,读下面字符,直到下一个引号前为止
int main()
{
     
        char source[100]  = {
     "how are you = \"nihao\""}char dest[100] = {
     0}int i;
        sscanf(source,"%*[^\"]"\%[^\"]",dest)//关键行
        printf("dest = %s\n",dest)return 0}

C++ 实现:

//跳过第一个引号以前的字符,读下面字符,直到下一个引号前为止
int main()
{
     
        string source = "how are you = \"nihao\""int pos_1 = source.find("\"")int pos_2 = source.find_last_of("\"");
        string dest  = "";
        dest = source.substr(pos_1 + 1 ,pos_2 - pos_1 - 1)//substr(t1,t2) //含义:从t1位置读取t2长度的字符串
        cout<<"dest = " <<dest<<endl;
}

有任何问题或有更好的方法欢迎讨论,谢谢!!!

你可能感兴趣的:(c语言,经验分享)