C语言 如何截断C char *?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6480440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:00:16  来源:igfitidea点击:

How to truncate C char*?

cstringchar

提问by erandros

As simple as that. I'm on C++ btw. I've read the cplusplus.com's cstdlib library functions, but I can't find a simple function for this. I know the length of the char, I only need to eraselast three characters from it. I can use C++ string, but this is for handling files, which uses char*, and I don't want to do conversions from string to C char.

就如此容易。顺便说一句,我在使用 C++。我已经阅读了 cplusplus.com 的 cstdlib 库函数,但是我找不到一个简单的函数。我知道字符的长度,我只需要从中删除最后三个字符。我可以使用 C++ 字符串,但这是用于处理使用 char* 的文件,我不想进行从字符串到 C 字符的转换。

回答by pmg

If you don't need to copy the string somewhere else and can change it

如果您不需要将字符串复制到其他地方并且可以更改它

/* make sure strlen(name) >= 3 */
namelen = strlen(name); /* possibly you've saved the length previously */
name[namelen - 3] = 0;

If you need to copy it (because it's a string literal or you want to keep the original around)

如果您需要复制它(因为它是一个字符串文字或者您想保留原始内容)

/* make sure strlen(name) >= 3 */
namelen = strlen(name); /* possibly you've saved the length previously */
strncpy(copy, name, namelen - 3);
/* add a final null terminator */
copy[namelen - 3] = 0;

回答by thelionroars1337

I think some of your post was lost in translation.

我认为您的一些帖子在翻译中丢失了。

To truncate a string in C, you can simply insert a terminating null character in the desired position. All of the standard functions will then treat the string as having the new length.

要在 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]  = '
const char* mystring = "abc123";
const int len = 6;

const char* substring = mystring + len - 3;
'; printf("%s\n", string); return 0; }

回答by Anders Abel

If you know the length of the string you can use pointer arithmetic to get a string with the last three characters:

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

bool TakeOutLastThreeChars(char* src, int len) {
  if (len < 3) return false;
  memset(src + len - 3, 0, 3);
  return true;
}

Please note that substringpoints to the same memory as mystringand is only valid as long as mystringis valid and left unchanged. The reason that this works is that a c string doesn't have any special markers at the beginning, only the NULLtermination at the end.

请注意,substring指向与相同的内存mystring并且仅mystring在有效且保持不变时才有效。这样做的原因是 ac 字符串在开头没有任何特殊标记,只有NULL结尾处有终止符。

I interpreted your question as wanting the last three characters, getting rid of the start, as opposed to how David Heffernan read it, one of us is obviously wrong.

我将你的问题解释为想要最后三个字符,去掉开头,而不是大卫赫弗南如何阅读它,我们中的一个人显然是错误的。

回答by cdiggins

char* str = "theFile.nam";

I assume mutating the string memory is safe since you did say erasethe last three characters. I'm just overwriting the last three characters with "NULL" or 0.

我认为改变字符串内存是安全的,因为您确实说过擦除最后三个字符。我只是用“NULL”或 0 覆盖了最后三个字符。

回答by trutheality

It might help to understand how C char*"strings" work:

它可能有助于理解 C char*“字符串”的工作原理:

You start reading them from the char that the char* points to until you hit a \0char (or simply 0).

您从 char* 指向的字符开始读取它们,直到遇到\0字符(或只是 0)。

So if I have

所以如果我有

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

then str+3represents the string File.nam.

然后str+3代表字符串File.nam

But you want to remove the last three characters, so you want something like:

但是您想删除最后三个字符,因此您需要以下内容:

##代码##