How do I trim leading/trailing whitespace in a standard way?

后端 未结 30 2004
一个人的身影
一个人的身影 2020-11-22 02:06

Is there a clean, preferably standard method of trimming leading and trailing whitespace from a string in C? I\'d roll my own, but I would think this is a common problem wit

相关标签:
30条回答
  • 2020-11-22 02:23

    I didn't like most of these answers because they did one or more of the following...

    1. Returned a different pointer inside the original pointer's string (kind of a pain to juggle two different pointers to the same thing).
    2. Made gratuitous use of things like strlen() that pre-iterate the entire string.
    3. Used non-portable OS-specific lib functions.
    4. Backscanned.
    5. Used comparison to ' ' instead of isspace() so that TAB / CR / LF are preserved.
    6. Wasted memory with large static buffers.
    7. Wasted cycles with high-cost functions like sscanf/sprintf.

    Here is my version:

    void fnStrTrimInPlace(char *szWrite) {
    
        const char *szWriteOrig = szWrite;
        char       *szLastSpace = szWrite, *szRead = szWrite;
        int        bNotSpace;
    
        // SHIFT STRING, STARTING AT FIRST NON-SPACE CHAR, LEFTMOST
        while( *szRead != '\0' ) {
    
            bNotSpace = !isspace((unsigned char)(*szRead));
    
            if( (szWrite != szWriteOrig) || bNotSpace ) {
    
                *szWrite = *szRead;
                szWrite++;
    
                // TRACK POINTER TO LAST NON-SPACE
                if( bNotSpace )
                    szLastSpace = szWrite;
            }
    
            szRead++;
        }
    
        // TERMINATE AFTER LAST NON-SPACE (OR BEGINNING IF THERE WAS NO NON-SPACE)
        *szLastSpace = '\0';
    }
    
    0 讨论(0)
  • 2020-11-22 02:25

    Here's one that shifts the string into the first position of your buffer. You might want this behavior so that if you dynamically allocated the string, you can still free it on the same pointer that trim() returns:

    char *trim(char *str)
    {
        size_t len = 0;
        char *frontp = str;
        char *endp = NULL;
    
        if( str == NULL ) { return NULL; }
        if( str[0] == '\0' ) { return str; }
    
        len = strlen(str);
        endp = str + len;
    
        /* Move the front and back pointers to address the first non-whitespace
         * characters from each end.
         */
        while( isspace((unsigned char) *frontp) ) { ++frontp; }
        if( endp != frontp )
        {
            while( isspace((unsigned char) *(--endp)) && endp != frontp ) {}
        }
    
        if( frontp != str && endp == frontp )
                *str = '\0';
        else if( str + len - 1 != endp )
                *(endp + 1) = '\0';
    
        /* Shift the string so that it starts at str so that if it's dynamically
         * allocated, we can still free it on the returned pointer.  Note the reuse
         * of endp to mean the front of the string buffer now.
         */
        endp = str;
        if( frontp != str )
        {
                while( *frontp ) { *endp++ = *frontp++; }
                *endp = '\0';
        }
    
        return str;
    }
    

    Test for correctness:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    /* Paste function from above here. */
    
    int main()
    {
        /* The test prints the following:
        [nothing to trim] -> [nothing to trim]
        [    trim the front] -> [trim the front]
        [trim the back     ] -> [trim the back]
        [    trim front and back     ] -> [trim front and back]
        [ trim one char front and back ] -> [trim one char front and back]
        [ trim one char front] -> [trim one char front]
        [trim one char back ] -> [trim one char back]
        [                   ] -> []
        [ ] -> []
        [a] -> [a]
        [] -> []
        */
    
        char *sample_strings[] =
        {
                "nothing to trim",
                "    trim the front",
                "trim the back     ",
                "    trim front and back     ",
                " trim one char front and back ",
                " trim one char front",
                "trim one char back ",
                "                   ",
                " ",
                "a",
                "",
                NULL
        };
        char test_buffer[64];
        char comparison_buffer[64];
        size_t index, compare_pos;
    
        for( index = 0; sample_strings[index] != NULL; ++index )
        {
            // Fill buffer with known value to verify we do not write past the end of the string.
            memset( test_buffer, 0xCC, sizeof(test_buffer) );
            strcpy( test_buffer, sample_strings[index] );
            memcpy( comparison_buffer, test_buffer, sizeof(comparison_buffer));
    
            printf("[%s] -> [%s]\n", sample_strings[index],
                                     trim(test_buffer));
    
            for( compare_pos = strlen(comparison_buffer);
                 compare_pos < sizeof(comparison_buffer);
                 ++compare_pos )
            {
                if( test_buffer[compare_pos] != comparison_buffer[compare_pos] )
                {
                    printf("Unexpected change to buffer @ index %u: %02x (expected %02x)\n",
                        compare_pos, (unsigned char) test_buffer[compare_pos], (unsigned char) comparison_buffer[compare_pos]);
                }
            }
        }
    
        return 0;
    }
    

    Source file was trim.c. Compiled with 'cc -Wall trim.c -o trim'.

    0 讨论(0)
  • 2020-11-22 02:25

    To trim my strings from the both sides I use the oldie but the gooody ;) It can trim anything with ascii less than a space, meaning that the control chars will be trimmed also !

    char *trimAll(char *strData)
    {
      unsigned int L = strlen(strData);
      if(L > 0){ L--; }else{ return strData; }
      size_t S = 0, E = L;
      while((!(strData[S] > ' ') || !(strData[E] > ' ')) && (S >= 0) && (S <= L) && (E >= 0) && (E <= L))
      {
        if(strData[S] <= ' '){ S++; }
        if(strData[E] <= ' '){ E--; }
      }
      if(S == 0 && E == L){ return strData; } // Nothing to be done
      if((S >= 0) && (S <= L) && (E >= 0) && (E <= L)){
        L = E - S + 1;
        memmove(strData,&strData[S],L); strData[L] = '\0';
      }else{ strData[0] = '\0'; }
      return strData;
    }
    
    0 讨论(0)
  • 2020-11-22 02:26

    Ok this is my take on the question. I believe it's the most concise solution that modifies the string in place (free will work) and avoids any UB. For small strings, it's probably faster than a solution involving memmove.

    void stripWS_LT(char *str)
    {
        char *a = str, *b = str;
        while (isspace((unsigned char)*a)) a++;
        while (*b = *a++)  b++;
        while (b > str && isspace((unsigned char)*--b)) *b = 0;
    }
    
    0 讨论(0)
  • 2020-11-22 02:26

    Here is how I do it. It trims the string in place, so no worry about deallocating a returned string or losing the pointer to an allocated string. It may not be the shortest answer possible, but it should be clear to most readers.

    #include <ctype.h>
    #include <string.h>
    void trim_str(char *s)
    {
        const size_t s_len = strlen(s);
    
        int i;
        for (i = 0; i < s_len; i++)
        {
            if (!isspace( (unsigned char) s[i] )) break;
        }
    
        if (i == s_len)
        {
            // s is an empty string or contains only space characters
    
            s[0] = '\0';
        }
        else
        {
            // s contains non-space characters
    
            const char *non_space_beginning = s + i;
    
            char *non_space_ending = s + s_len - 1;
            while ( isspace( (unsigned char) *non_space_ending ) ) non_space_ending--;
    
            size_t trimmed_s_len = non_space_ending - non_space_beginning + 1;
    
            if (s != non_space_beginning)
            {
                // Non-space characters exist in the beginning of s
    
                memmove(s, non_space_beginning, trimmed_s_len);
            }
    
            s[trimmed_s_len] = '\0';
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:27

    If you can modify the string:

    // Note: This function returns a pointer to a substring of the original string.
    // If the given string was allocated dynamically, the caller must not overwrite
    // that pointer with the returned value, since the original pointer must be
    // deallocated using the same allocator with which it was allocated.  The return
    // value must NOT be deallocated using free() etc.
    char *trimwhitespace(char *str)
    {
      char *end;
    
      // Trim leading space
      while(isspace((unsigned char)*str)) str++;
    
      if(*str == 0)  // All spaces?
        return str;
    
      // Trim trailing space
      end = str + strlen(str) - 1;
      while(end > str && isspace((unsigned char)*end)) end--;
    
      // Write new null terminator character
      end[1] = '\0';
    
      return str;
    }
    

    If you can't modify the string, then you can use basically the same method:

    // Stores the trimmed input string into the given output buffer, which must be
    // large enough to store the result.  If it is too small, the output is
    // truncated.
    size_t trimwhitespace(char *out, size_t len, const char *str)
    {
      if(len == 0)
        return 0;
    
      const char *end;
      size_t out_size;
    
      // Trim leading space
      while(isspace((unsigned char)*str)) str++;
    
      if(*str == 0)  // All spaces?
      {
        *out = 0;
        return 1;
      }
    
      // Trim trailing space
      end = str + strlen(str) - 1;
      while(end > str && isspace((unsigned char)*end)) end--;
      end++;
    
      // Set output size to minimum of trimmed string length and buffer size minus 1
      out_size = (end - str) < len-1 ? (end - str) : len-1;
    
      // Copy trimmed string and add null terminator
      memcpy(out, str, out_size);
      out[out_size] = 0;
    
      return out_size;
    }
    
    0 讨论(0)
提交回复
热议问题