我正在编写一个C程序,目前遇到了问题。我不知道如何删除字符串的最后一部分。例如:
char str[100] = "one => two";
我想要删除=> two
。你能帮我吗?谢谢。
char
标记后的部分,请使用以下方法:char str[100] = "one => two";
char *temp;
temp = strchr(str,'='); //Get the pointer to char token
*temp = '\0'; //Replace token with null char
找到紧跟着"one"
的空格位置,然后用'\0'
替换它。
str[3] = 0;
int i = 0;
char newstr [100];
while (str[i] != '='){
i++;
}
strncpy (newstr, str, i); //copy the i first characters from a char [] to a new char []
newstr [i] = 0;