Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何截断C字符*?_C_String_Char - Fatal编程技术网

如何截断C字符*?

如何截断C字符*?,c,string,char,C,String,Char,就这么简单。我在C++ BTW.我已经阅读了CPLUS PLUS .com的CSTDLIB库函数,但是我找不到一个简单的函数。 我知道字符的长度,我只需要从中删除最后三个字符。我可以使用C++字符串,但是这是用来处理文件的,它使用char *,而且我不想做从字符串到C char的转换。< p>如果你不需要在其他地方复制字符串,可以改变它 /* make sure strlen(name) >= 3 */ namelen = strlen(name); /* possibly you've

就这么简单。我在C++ BTW.我已经阅读了CPLUS PLUS .com的CSTDLIB库函数,但是我找不到一个简单的函数。
我知道字符的长度,我只需要从中删除最后三个字符。我可以使用C++字符串,但是这是用来处理文件的,它使用char *,而且我不想做从字符串到C char的转换。

< p>如果你不需要在其他地方复制字符串,可以改变它

/* make sure strlen(name) >= 3 */
namelen = strlen(name); /* possibly you've saved the length previously */
name[namelen - 3] = 0;
如果您需要复制它(因为它是字符串文字,或者您希望保留原始内容)


如果您知道字符串的长度,可以使用指针算法获取包含最后三个字符的字符串:

const char* mystring = "abc123";
const int len = 6;

const char* substring = mystring + len - 3;
请注意,
substring
指向与
mystring
相同的内存,并且只在
mystring
有效且保持不变时才有效。这样做的原因是c字符串在开头没有任何特殊标记,只有结尾的
NULL
终止

我把你的问题解释为想要最后三个字符,去掉开头,而不是大卫·赫弗南(David Heffernan)的阅读方式,我们中的一个显然是错的。

bool TakeOutLastThreeChars(char*src,int len){
如果(len<3)返回false;
memset(src+len-3,0,3);
返回true;
}

我假设对字符串内存进行变异是安全的,因为您确实说过删除最后三个字符。我只是用“NULL”或0覆盖最后三个字符

了解C
char*
字符串的工作原理可能会有所帮助:

从char*指向的char开始读取它们,直到找到
\0
char(或者干脆是0)

所以如果我有

char* str = "theFile.nam";
然后
str+3
表示字符串
File.nam

但您希望删除最后三个字符,因此需要类似以下内容:

char str2[9];
strncpy (str2,str,8); // now str2 contains "theFile.#" where # is some character you don't know about
str2[8]='\0'; // now str2 contains "theFile.\0" and is a proper char* string.

我想你的一些帖子在翻译中丢失了

要截断C中的字符串,只需在所需位置插入一个终止空字符。然后,所有标准函数都会将字符串视为具有新长度

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

int main(void)
{
    char string[] = "one one two three five eight thirteen twenty-one";

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

    string[strlen(string) - 3]  = '\0';

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

    return 0;
}
#包括
#包括
内部主(空)
{
字符字符串[]=“一一二三五八一三二一”;
printf(“%s\n”,字符串);
字符串[strlen(string)-3]='\0';
printf(“%s\n”,字符串);
返回0;
}

memset
似乎有点强
char str2[9];
strncpy (str2,str,8); // now str2 contains "theFile.#" where # is some character you don't know about
str2[8]='\0'; // now str2 contains "theFile.\0" and is a proper char* string.
#include <stdio.h>
#include <string.h>

int main(void)
{
    char string[] = "one one two three five eight thirteen twenty-one";

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

    string[strlen(string) - 3]  = '\0';

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

    return 0;
}