Search code examples
cstringcharstripansi-c

Remove text from string after a certain character in C


I am reading lines from a file, the lines look like this:

89f81a03eb30a03c8708dde38cf:000391716

The thing is: I want to remove everything after the : (including the :). I tried everything I could find online but they seem to use const char and the lines are char pointers.


Solution

  • You can use strchr:

    char str[] = "89f81a03eb30a03c8708dde38cf:000391716";
    char *ptr;
    
    ptr = strchr(str, ':');
    if (ptr != NULL) {
        *ptr = '\0';
    }