简体   繁体   中英

How to truncate C char*?

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 erase last 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.

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. 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. 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.

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.

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.

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

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

So if I have

char* str = "theFile.nam";

then str+3 represents the string 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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