简体   繁体   English

如何截断C char *?

[英]How to truncate C char*?

As simple as that. 就如此容易。 I'm on C++ btw. 我正在使用C ++ btw。 I've read the cplusplus.com's cstdlib library functions, but I can't find a simple function for this. 我已经阅读了cplusplus.com的cstdlib库函数,但是我找不到一个简单的函数。 I know the length of the char, I only need to erase last three characters from it. 我知道char的长度,我只需要删除它的最后三个字符。 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 ++字符串,但这是用于处理使用char *的文件,我不想从字符串转换为C char。

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;

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. 要在C中截断字符串,只需在所需位置插入一个终止空字符即可。 All of the standard functions will then treat the string as having the new length. 然后,所有标准函数都将字符串视为具有新长度。

#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;
}

If you know the length of the string you can use pointer arithmetic to get a string with the last three characters: 如果知道字符串的长度,可以使用指针算法来获取最后三个字符的字符串:

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

const char* substring = mystring + len - 3;

Please note that substring points to the same memory as mystring and is only valid as long as mystring is valid and left unchanged. 请注意, substring指向与mystring相同的内存,只有mystring有效且保持不变才有效。 The reason that this works is that ac string doesn't have any special markers at the beginning, only the NULL termination at the end. 这样做的原因是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. 我把你的问题解释为想要最后三个字符,摆脱起点,而不是David Heffernan如何读它,我们其中一个人显然是错的。

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

I assume mutating the string memory is safe since you did say erase the last three characters. 我假设改变字符串内存是安全的,因为你说删除最后三个字符。 I'm just overwriting the last three characters with "NULL" or 0. 我只是用“NULL”或0覆盖最后三个字符。

It might help to understand how C char* "strings" work: 它可能有助于理解C char* “strings”的工作原理:

You start reading them from the char that the char* points to until you hit a \\0 char (or simply 0). 你开始从char *指向的char中读取它们,直到你达到\\0 char(或简称为0)。

So if I have 所以,如果我有

char* str = "theFile.nam";

then str+3 represents the string File.nam . 然后str+3表示字符串File.nam

But you want to remove the last three characters, so you want something like: 但是你想删除最后三个字符,所以你需要这样的东西:

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.

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

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