是否有一个清洁,最好是标准方法的修剪头和结尾的空白,从一串在C?我滚是我自己,但我认为这是一个常见的问题,与一个同样共同解决方案。

有帮助吗?

解决方案

如果你可以修改字符串:

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

如果你不能修改字符串,那么你可以使用基本相同的方法:

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

其他提示

这是将字符串移动到缓冲区的第一个位置的一个。您可能希望这种行为,以便如果您动态分配字符串,您仍然可以在trim()返回的同一指针上释放它:

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( str + len - 1 != endp )
            *(endp + 1) = '\0';
    else if( frontp != str &&  endp == frontp )
            *str = '\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;
}

测试正确性:

int main(int argc, char *argv[])
{
    char *sample_strings[] =
    {
            "nothing to trim",
            "    trim the front",
            "trim the back     ",
            " trim one char front and back ",
            " trim one char front",
            "trim one char back ",
            "                   ",
            " ",
            "a",
            "",
            NULL
    };
    char test_buffer[64];
    int index;

    for( index = 0; sample_strings[index] != NULL; ++index )
    {
            strcpy( test_buffer, sample_strings[index] );
            printf("[%s] -> [%s]\n", sample_strings[index],
                                     trim(test_buffer));
    }

    /* The test prints the following:
    [nothing to trim] -> [nothing to trim]
    [    trim the front] -> [trim the front]
    [trim the back     ] -> [trim the 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]
    [] -> []
    */

    return 0;
}

源文件是trim.c.用'cc trim.c -o trim'编译。

我的解决方案。字符串必须是可更改的。一些其他解决方案的优势在于它将非空间部分移动到开头,因此您可以继续使用旧指针,以防您以后必须释放它()。

void trim(char * s) {
    char * p = s;
    int l = strlen(p);

    while(isspace(p[l - 1])) p[--l] = 0;
    while(* p && isspace(* p)) ++p, --l;

    memmove(s, p, l + 1);
}   

此版本使用strndup()创建字符串的副本,而不是在适当位置编辑它。 strndup()需要_GNU_SOURCE,所以也许你需要使用malloc()和strncpy()创建自己的strndup()。

char * trim(char * s) {
    int l = strlen(s);

    while(isspace(s[l - 1])) --l;
    while(* s && isspace(* s)) ++s, --l;

    return strndup(s, l);
}

这是我的C迷你库,用于修剪左,右,两者,全部,就地和分离,并修剪一组指定的字符(或默认为空格)。

strlib.h的内容:

#ifndef STRLIB_H_
#define STRLIB_H_ 1
enum strtrim_mode_t {
    STRLIB_MODE_ALL       = 0, 
    STRLIB_MODE_RIGHT     = 0x01, 
    STRLIB_MODE_LEFT      = 0x02, 
    STRLIB_MODE_BOTH      = 0x03
};

char *strcpytrim(char *d, // destination
                 char *s, // source
                 int mode,
                 char *delim
                 );

char *strtriml(char *d, char *s);
char *strtrimr(char *d, char *s);
char *strtrim(char *d, char *s); 
char *strkill(char *d, char *s);

char *triml(char *s);
char *trimr(char *s);
char *trim(char *s);
char *kill(char *s);
#endif

strlib.c的内容:

#include <strlib.h>

char *strcpytrim(char *d, // destination
                 char *s, // source
                 int mode,
                 char *delim
                 ) {
    char *o = d; // save orig
    char *e = 0; // end space ptr.
    char dtab[256] = {0};
    if (!s || !d) return 0;

    if (!delim) delim = " \t\n\f";
    while (*delim) 
        dtab[*delim++] = 1;

    while ( (*d = *s++) != 0 ) { 
        if (!dtab[0xFF & (unsigned int)*d]) { // Not a match char
            e = 0;       // Reset end pointer
        } else {
            if (!e) e = d;  // Found first match.

            if ( mode == STRLIB_MODE_ALL || ((mode != STRLIB_MODE_RIGHT) && (d == o)) ) 
                continue;
        }
        d++;
    }
    if (mode != STRLIB_MODE_LEFT && e) { // for everything but trim_left, delete trailing matches.
        *e = 0;
    }
    return o;
}

// perhaps these could be inlined in strlib.h
char *strtriml(char *d, char *s) { return strcpytrim(d, s, STRLIB_MODE_LEFT, 0); }
char *strtrimr(char *d, char *s) { return strcpytrim(d, s, STRLIB_MODE_RIGHT, 0); }
char *strtrim(char *d, char *s) { return strcpytrim(d, s, STRLIB_MODE_BOTH, 0); }
char *strkill(char *d, char *s) { return strcpytrim(d, s, STRLIB_MODE_ALL, 0); }

char *triml(char *s) { return strcpytrim(s, s, STRLIB_MODE_LEFT, 0); }
char *trimr(char *s) { return strcpytrim(s, s, STRLIB_MODE_RIGHT, 0); }
char *trim(char *s) { return strcpytrim(s, s, STRLIB_MODE_BOTH, 0); }
char *kill(char *s) { return strcpytrim(s, s, STRLIB_MODE_ALL, 0); }

一个主要例程就是这一切。 如果 src == dst ,它会修剪到位,否则, 它的工作方式与 strcpy 例程类似。 它修剪字符串 delim 中指定的一组字符,如果为null则修剪为空格。 它修剪左,右,两个和所有(如tr)。 没有多少,它只在字符串上迭代一次。有些人可能会抱怨左边的修剪右边开始,但是,无论如何都不需要从左边开始的修剪。 (不管怎样,你必须到达字符串的末尾才能进行正确的修剪,所以你可以随心所欲地完成工作。)可能会有关于流水线和缓存大小的争论等等 - 谁知道。由于解决方案从左到右工作并且只迭代一次,因此它也可以扩展为在流上工作。限制: unicode 字符串有效。

这是我尝试使用简单但正确的就地修剪功能。

void trim(char *str)
{
    int i;
    int begin = 0;
    int end = strlen(str) - 1;

    while (isspace((unsigned char) str[begin]))
        begin++;

    while ((end >= begin) && isspace((unsigned char) str[end]))
        end--;

    // Shift all characters back to the start of the string array.
    for (i = begin; i <= end; i++)
        str[i - begin] = str[i];

    str[i - begin] = '\0'; // Null terminate string.
}

装饰派对迟到了

特点:结果  1.快速修剪开头,就像其他一些答案一样。
 2.结束后,每个循环只进行1次测试即可修剪右边。像@ jfm3,但适用于所有白色空格字符串)
 3.当 char 是带符号的 char 时,为了避免未定义的行为,将 * s 强制转换为 unsigned char

  

字符处理&quot;在所有情况下,参数都是 int ,其值应表示为 unsigned char 或者等于宏 EOF 的值。如果参数具有任何其他值,则行为未定义。 C11&#167; 7.4 1

#include <ctype.h>

// Return a pointer to the trimmed string
char *string_trim_inplace(char *s) {
  while (isspace((unsigned char) *s)) s++;
  if (*s) {
    char *p = s;
    while (*p) p++;
    while (isspace((unsigned char) *(--p)));
    p[1] = '\0';
  }

  // If desire to shift the trimmed string

  return s;
}

@chqrlie 评论说上面的内容并没有改变修剪过的字符串。这样做....

// Return a pointer to the (shifted) trimmed string
char *string_trim_inplace(char *s) {
  char *original = s;
  size_t len = 0;

  while (isspace((unsigned char) *s)) {
    s++;
  } 
  if (*s) {
    char *p = s;
    while (*p) p++;
    while (isspace((unsigned char) *(--p)));
    p[1] = '\0';
    len = (size_t) (p - s);
  }

  return (s == original) ? s : memove(original, s, len + 1);
}

这是一个类似于@adam-rosenfields就地修改例程的解决方案,但没有不必要地使用strlen()。与@jkramer一样,字符串在缓冲区内左调整,因此您可以释放相同的指针。对于大字符串不是最佳的,因为它不使用memmove。包括@jfm3提到的++ / - 运算符。包括基于FCTX 的单元测试。

#include <ctype.h>

void trim(char * const a)
{
    char *p = a, *q = a;
    while (isspace(*q))            ++q;
    while (*q)                     *p++ = *q++;
    *p = '\0';
    while (p > a && isspace(*--p)) *p = '\0';
}

/* See http://fctx.wildbearsoftware.com/ */
#include "fct.h"

FCT_BGN()
{
    FCT_QTEST_BGN(trim)
    {
        { char s[] = "";      trim(s); fct_chk_eq_str("",    s); } // Trivial
        { char s[] = "   ";   trim(s); fct_chk_eq_str("",    s); } // Trivial
        { char s[] = "\t";    trim(s); fct_chk_eq_str("",    s); } // Trivial
        { char s[] = "a";     trim(s); fct_chk_eq_str("a",   s); } // NOP
        { char s[] = "abc";   trim(s); fct_chk_eq_str("abc", s); } // NOP
        { char s[] = "  a";   trim(s); fct_chk_eq_str("a",   s); } // Leading
        { char s[] = "  a c"; trim(s); fct_chk_eq_str("a c", s); } // Leading
        { char s[] = "a  ";   trim(s); fct_chk_eq_str("a",   s); } // Trailing
        { char s[] = "a c  "; trim(s); fct_chk_eq_str("a c", s); } // Trailing
        { char s[] = " a ";   trim(s); fct_chk_eq_str("a",   s); } // Both
        { char s[] = " a c "; trim(s); fct_chk_eq_str("a c", s); } // Both

        // Villemoes pointed out an edge case that corrupted memory.  Thank you.
        // http://stackoverflow.com/questions/122616/#comment23332594_4505533
        {
          char s[] = "a     ";       // Buffer with whitespace before s + 2
          trim(s + 2);               // Trim "    " containing only whitespace
          fct_chk_eq_str("", s + 2); // Ensure correct result from the trim
          fct_chk_eq_str("a ", s);   // Ensure preceding buffer not mutated
        }

        // doukremt suggested I investigate this test case but
        // did not indicate the specific behavior that was objectionable.
        // http://stackoverflow.com/posts/comments/33571430
        {
          char s[] = "         foobar";  // Shifted across whitespace
          trim(s);                       // Trim
          fct_chk_eq_str("foobar", s);   // Leading string is correct

          // Here is what the algorithm produces:
          char r[16] = { 'f', 'o', 'o', 'b', 'a', 'r', '\0', ' ',                     
                         ' ', 'f', 'o', 'o', 'b', 'a', 'r', '\0'};
          fct_chk_eq_int(0, memcmp(s, r, sizeof(s)));
        }
    }
    FCT_QTEST_END();
}
FCT_END();

另一个,有一条线做真正的工作:

#include <stdio.h>

int main()
{
   const char *target = "   haha   ";
   char buf[256];
   sscanf(target, "%s", buf); // Trimming on both sides occurs here
   printf("<%s>\n", buf);
}

我没有像大多数的这些问题的答案,因为他们没有一个或多个下...

  1. 返回的一个不同的内部指针的原始指的串(一种止痛兼顾两个不同的指针指向同事)。
  2. 由免费使用之类的东西 strlen() 预循环的整个弦。
  3. 使用非便携式的操作系统的具体lib功能。
  4. Backscanned.
  5. 用于比较 ' ' 而不是的 isspace() 因此,TAB/CR/LF被保留。
  6. 浪费存储器与大型固的缓冲器。
  7. 浪费的周期与高成本的功能喜欢 sscanf/sprintf.

这里是我的版本:

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

聚会很晚......

单通道正向扫描解决方案,无回溯。源字符串中的每个字符都经过一次两次测试。 (所以它应该比其他大多数解决方案更快,特别是如果源字符串有很多尾随空格。)

这包括两个解决方案,一个用于将源字符串复制并修剪为另一个目标字符串,另一个用于修剪源字符串。两个函数都使用相同的代码。

(可修改的)字符串就地移动,因此指向它的原始指针保持不变。

#include <stddef.h>
#include <ctype.h>

char * trim2(char *d, const char *s)
{
    // Sanity checks
    if (s == NULL  ||  d == NULL)
        return NULL;

    // Skip leading spaces        
    const unsigned char * p = (const unsigned char *)s;
    while (isspace(*p))
        p++;

    // Copy the string
    unsigned char * dst = (unsigned char *)d;   // d and s can be the same
    unsigned char * end = dst;
    while (*p != '\0')
    {
        if (!isspace(*dst++ = *p++))
            end = dst;
    }

    // Truncate trailing spaces
    *end = '\0';
    return d;
}

char * trim(char *s)
{
    return trim2(s, s);
}

我不确定你认为什么是“无痛的”。

C字符串非常痛苦。我们可以通过以下方式找到第一个非空白字符位置:

while (isspace(* p)) p++;

我们可以通过两个相似的小动作找到最后一个非空白字符位置:

while (* q) q++;
do { q--; } while (isspace(* q));

(我已经免除了你同时使用 * ++ 运算符的痛苦。)

现在的问题是你对此有何看法?手头的数据类型实际上并不是一个很容易想到的强大的抽象 String ,而是几乎不过是一个存储字节数组。缺乏强大的数据类型,不可能编写与PHperytonby的 chomp 函数相同的函数。 C中的这样一个函数会返回什么?

使用字符串库,例如:

Ustr *s1 = USTR1(\7, " 12345 ");

ustr_sc_trim_cstr(&s1, " ");
assert(ustr_cmp_cstr_eq(s1, "12345"));

...正如你所说这是一个“普通”的问题,是的,你需要包含一个#include左右,并且它不包含在libc中,但是不要发明你自己的黑客作业存储随机指针和size_t这样只会导致缓冲区溢出。

#include "stdafx.h"
#include "malloc.h"
#include "string.h"

int main(int argc, char* argv[])
{

  char *ptr = (char*)malloc(sizeof(char)*30);
  strcpy(ptr,"            Hel  lo    wo           rl   d G    eo rocks!!!    by shahil    sucks b i          g       tim           e");

  int i = 0, j = 0;

  while(ptr[j]!='\0')
  {

      if(ptr[j] == ' ' )
      {
          j++;
          ptr[i] = ptr[j];
      }
      else
      {
          i++;
          j++;
          ptr[i] = ptr[j];
      }
  }


  printf("\noutput-%s\n",ptr);
        return 0;
}

s非常有帮助,我想说我很高兴这篇文章可用,并展示我能够用这些例子做些什么。我需要标记一个更大的字符串,然后获取子字符串并找到最后一个 - 所以我可以从fgets()调用中删除换行符,并从该标记的前面删除空格 - 所以我可以很容易将它与静态字符串进行比较。上面帖子中的第一个例子让我在那里,谢谢。以下是我使用代码示例和输出的方法。

int _tmain(int argc, _TCHAR* argv[])
{
   FILE * fp;   // test file
   char currDBSStatstr[100] = {"/0"};
   char *beg;
   char *end;
   char *str1;
   char str[] = "Initializing DBS Configuration";
   fp = fopen("file2-1.txt","r");
   if (fp != NULL)
   {
      printf("File exists.\n");
      fgets(currDBSStatstr, sizeof(currDBSStatstr), fp);
   }
   else
   {
      printf("Error.\n");
      exit(2);
   }  
   //print string
   printf("String: %s\n", currDBSStatstr);
   //extract first string
   str1 = strtok(currDBSStatstr, ":-");
   //print first token
   printf("%s\n", str1);
   //get more tokens in sequence
   while(1)
   {
      //extract more tokens in sequence
      str1 = strtok(NULL, ":-");
      //check to see if done
      if (str1 == NULL)
      {
         printf("Tokenizing Done.\n");
         exit(0);
      }
      //print string after tokenizing Done
      printf("%s\n", str1);
      end = str1 + strlen(str1) - 1;
      while((end > str1) && (*end == '\n'))
      {
         end--;
         *(end+1) = 0;
         beg = str1;
         while(isspace(*str1))
            str1++;
      }
      printf("%s\n", str1);
      if (strcmp(str, str1) == 0)
         printf("Strings are equal.\n");
   }
   return 0;

}

<强>输出

  

档案存在。

     

字符串:DBS状态:DBS启动 - 初始化DBS配置

     

DBS State

     

DBS Startup

     

DBS Startup

     

初始化DBS配置

     

初始化DBS配置

     

字符串相等。

     

Tokenizing Done。

如果你正在使用 glib ,那么你可以使用 g_strstrip

为了保持这种增长,还有一个带有可修改字符串的选项:

void trimString(char *string)
{
    size_t i = 0, j = strlen(string);
    while (j > 0 && isspace((unsigned char)string[j - 1])) string[--j] = '\0';
    while (isspace((unsigned char)string[i])) i++;
    if (i > 0) memmove(string, string + i, j - i + 1);
}

我知道有很多答案,但我在这里发布我的答案,看看我的解决方案是否足够好。

// Trims leading whitespace chars in left `str`, then copy at almost `n - 1` chars
// into the `out` buffer in which copying might stop when the first '\0' occurs, 
// and finally append '\0' to the position of the last non-trailing whitespace char.
// Reture the length the trimed string which '\0' is not count in like strlen().
size_t trim(char *out, size_t n, const char *str)
{
    // do nothing
    if(n == 0) return 0;    

    // ptr stop at the first non-leading space char
    while(isspace(*str)) str++;    

    if(*str == '\0') {
        out[0] = '\0';
        return 0;
    }    

    size_t i = 0;    

    // copy char to out until '\0' or i == n - 1
    for(i = 0; i < n - 1 && *str != '\0'; i++){
        out[i] = *str++;
    }    

    // deal with the trailing space
    while(isspace(out[--i]));    

    out[++i] = '\0';
    return i;
}

跳过字符串中前导空格的最简单方法是,imho,

#include <stdio.h>

int main()
{
char *foo="     teststring      ";
char *bar;
sscanf(foo,"%s",bar);
printf("String is >%s<\n",bar);
    return 0;
}

好的,这是我对这个问题的看法。我相信这是最简洁的解决方案,可以修改字符串( free 将起作用)并避免任何UB。对于小字符串,它可能比涉及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;
}
#include <ctype.h>
#include <string.h>

char *trim_space(char *in)
{
    char *out = NULL;
    int len;
    if (in) {
        len = strlen(in);
        while(len && isspace(in[len - 1])) --len;
        while(len && *in && isspace(*in)) ++in, --len;
        if (len) {
            out = strndup(in, len);
        }
    }
    return out;
}

isspace 有助于所有修剪白色的空间。

  • 运行的第一循环检查最后一个字节的空间角色和减少长度变量
  • 运行一个第二环,以检查从第一个字节的空间角色和减少长度的变量和增加char指针。
  • 最后,如果长度的变量超过0,然后使用 strndup 创造新的串通过缓冲区排除的空间。

就个人而言,我会自己动手。你可以使用strtok,但你需要注意这样做(特别是如果你要删除主角)你知道什么是内存。

摆脱尾随空格很简单,也很安全,因为你可以在最后一个空格的顶部加一个0,从最后算起。摆脱领先的空间意味着搬家。如果你想在适当的位置(可能是明智的)你可以继续将所有东西都移回一个角色,直到没有领先的空间。或者,为了提高效率,您可以找到第一个非空格字符的索引,并将所有内容移回该数字。或者,您可以使用指向第一个非空格字符的指针(但是您需要小心,就像使用strtok一样)。

比赛有点晚了,但我会将我的惯例投入到战斗中。它们可能不是最有效的,但我相信它们是正确的并且它们很简单(使用 rtrim()推动复杂性包络):

#include <ctype.h>
#include <string.h>

/*
    Public domain implementations of in-place string trim functions

    Michael Burr
    michael.burr@nth-element.com
    2010
*/

char* ltrim(char* s) 
{
    char* newstart = s;

    while (isspace( *newstart)) {
        ++newstart;
    }

    // newstart points to first non-whitespace char (which might be '\0')
    memmove( s, newstart, strlen( newstart) + 1); // don't forget to move the '\0' terminator

    return s;
}


char* rtrim( char* s)
{
    char* end = s + strlen( s);

    // find the last non-whitespace character
    while ((end != s) && isspace( *(end-1))) {
            --end;
    }

    // at this point either (end == s) and s is either empty or all whitespace
    //      so it needs to be made empty, or
    //      end points just past the last non-whitespace character (it might point
    //      at the '\0' terminator, in which case there's no problem writing
    //      another there).    
    *end = '\0';

    return s;
}

char*  trim( char* s)
{
    return rtrim( ltrim( s));
}

到目前为止,大多数答案都会执行以下操作之一:

  1. 在字符串末尾的Backtrack(即找到字符串的结尾,然后向后搜索,直到找到非空格字符)或
  2. 首先调用 strlen(),再次传递整个字符串。
  3. 此版本只进行一次传递,不会回溯。因此它可能比其他的更好,但前提是有几百个尾随空格(在处理SQL查询的输出时这并不罕见。)

    static char const WHITESPACE[] = " \t\n\r";
    
    static void get_trim_bounds(char  const *s,
                                char const **firstWord,
                                char const **trailingSpace)
    {
        char const *lastWord;
        *firstWord = lastWord = s + strspn(s, WHITESPACE);
        do
        {
            *trailingSpace = lastWord + strcspn(lastWord, WHITESPACE);
            lastWord = *trailingSpace + strspn(*trailingSpace, WHITESPACE);
        }
        while (*lastWord != '\0');
    }
    
    char *copy_trim(char const *s)
    {
        char const *firstWord, *trailingSpace;
        char *result;
        size_t newLength;
    
        get_trim_bounds(s, &firstWord, &trailingSpace);
        newLength = trailingSpace - firstWord;
    
        result = malloc(newLength + 1);
        memcpy(result, firstWord, newLength);
        result[newLength] = '\0';
        return result;
    }
    
    void inplace_trim(char *s)
    {
        char const *firstWord, *trailingSpace;
        size_t newLength;
    
        get_trim_bounds(s, &firstWord, &trailingSpace);
        newLength = trailingSpace - firstWord;
    
        memmove(s, firstWord, newLength);
        s[newLength] = '\0';
    }
    

这是我能想到的最短的实施方式:

static const char *WhiteSpace=" \n\r\t";
char* trim(char *t)
{
    char *e=t+(t!=NULL?strlen(t):0);               // *e initially points to end of string
    if (t==NULL) return;
    do --e; while (strchr(WhiteSpace, *e) && e>=t);  // Find last char that is not \r\n\t
    *(++e)=0;                                      // Null-terminate
    e=t+strspn (t,WhiteSpace);                           // Find first char that is not \t
    return e>t?memmove(t,e,strlen(e)+1):t;                  // memmove string contents and terminator
}

这些函数会修改原始缓冲区,因此如果动态分配,则为原始缓冲区 指针可以被释放。

#include <string.h>

void rstrip(char *string)
{
  int l;
  if (!string)
    return;
  l = strlen(string) - 1;
  while (isspace(string[l]) && l >= 0)
    string[l--] = 0;
}

void lstrip(char *string)
{
  int i, l;
  if (!string)
    return;
  l = strlen(string);
  while (isspace(string[(i = 0)]))
    while(i++ < l)
      string[i-1] = string[i];
}

void strip(char *string)
{
  lstrip(string);
  rstrip(string);
}

您对使用标头Shlwapi.h中定义的StrTrim函数有何看法?它是直接的,而不是自己定义。点击 详细信息可以在:
  http://msdn.microsoft。 com / zh-CN / library / windows / desktop / bb773454(v = vs.85).aspx

如果你有
char ausCaptain [] =&quot; GeorgeBailey&quot ;;
StrTrim(ausCaptain,&quot;&quot;);
这将 ausCaptain 作为&quot; GeorgeBailey&quot; 而不是&quot; GeorgeBailey&quot;

要从两边修剪我的琴弦,我会使用老人但是gooody;) 它可以修剪ascii小于空格的任何东西,这意味着控制字符也将被修剪!

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

我只包含代码,因为到目前为止发布的代码似乎不是最理想的(我还没有代表发表评论。)

void inplace_trim(char* s)
{
    int start, end = strlen(s);
    for (start = 0; isspace(s[start]); ++start) {}
    if (s[start]) {
        while (end > 0 && isspace(s[end-1]))
            --end;
        memmove(s, &s[start], end - start);
    }
    s[end - start] = '\0';
}

char* copy_trim(const char* s)
{
    int start, end;
    for (start = 0; isspace(s[start]); ++start) {}
    for (end = strlen(s); end > 0 && isspace(s[end-1]); --end) {}
    return strndup(s + start, end - start);
}

strndup()是一个GNU扩展。如果你没有它或类似的东西,请自己动手。例如:

r = strdup(s + start);
r[end-start] = '\0';

这里我使用动态内存分配将输入字符串修剪为函数trimStr。首先,我们发现输入字符串中存在多少个非空字符。然后,我们分配一个具有该大小的字符数组并处理空终止字符。当我们使用这个函数时,我们需要释放main函数内部的内存。

#include<stdio.h>
#include<stdlib.h>

char *trimStr(char *str){
char *tmp = str;
printf("input string %s\n",str);
int nc = 0;

while(*tmp!='\0'){
  if (*tmp != ' '){
  nc++;
 }
 tmp++;
}
printf("total nonempty characters are %d\n",nc);
char *trim = NULL;

trim = malloc(sizeof(char)*(nc+1));
if (trim == NULL) return NULL;
tmp = str;
int ne = 0;

while(*tmp!='\0'){
  if (*tmp != ' '){
     trim[ne] = *tmp;
   ne++;
 }
 tmp++;
}
trim[nc] = '\0';

printf("trimmed string is %s\n",trim);

return trim; 
 }


int main(void){

char str[] = " s ta ck ove r fl o w  ";

char *trim = trimStr(str);

if (trim != NULL )free(trim);

return 0;
}

我是这样做的。它修剪了字符串,因此不必担心释放返回的字符串或丢失指向已分配字符串的指针。它可能不是最短的答案,但大多数读者都应该清楚。

#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';
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top