繁体   English   中英

从字符串数组中删除第一个和最后一个字符

[英]Removing First and Last Character from string array

你好我试图找到一种方法来删除字符串数组的第一个和最后一个字符。

我的数据洞察数组是这样的:

“时间1”:

“3.0”

等等

我试图专门删除双引号和字符:我应该使用什么?

char item[100]; 
   char  LastArray[1000];
   while (NewMixedArray[i] != NULL)
   {
    sscanf (NewMixedArray[i],"\"%99[^\"]", item);
    LastArray[i]=item;
    printf("%d %s\n",i,LastArray[i]);
    i++;
   }

   i=0;
   while (  LastArray[i] != NULL)
   {
    printf("DONE: %d %s\n",i,LastArray[i]);
    i++;
   }

在第二个 printf 上,我只得到最后一个值。

https://i.imgur.com/H2OFz1Q.png

我发现我的问题我使用了命令

LastArray[i] = strdup(item); 因为我的 LastArray 是一个指针数组。

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

int main() {
    char string[1000], result[1000];
    strcpy(string, "\"Time1\":");

    printf("\nInitial String : %s", string);
    int i;
    int c = 0;
    do {
        if (string[i] != '\"' & string[i] != ':') {
            result[c] = string[i];
            c++;
        }
        i++;
    } while (string[i] != '\0');

    printf("\nResult : %s\n", result);
    return 0;
}

结果

$ ./ss

Initial String : "Time1":
Result : Time1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM