JRL*_*JRL 12
这让我想写自己的 - 我不喜欢那些提供的.在我看来应该有3个功能.
char *ltrim(char *s)
{
while(isspace(*s)) s++;
return s;
}
char *rtrim(char *s)
{
char* back = s + strlen(s);
while(isspace(*--back));
*(back+1) = '\0';
return s;
}
char *trim(char *s)
{
return rtrim(ltrim(s));
}
Run Code Online (Sandbox Code Playgroud)
惊讶地看到这样的实现。我通常是这样修剪的:
char *trim(char *s) {
char *ptr;
if (!s)
return NULL; // handle NULL string
if (!*s)
return s; // handle empty string
for (ptr = s + strlen(s) - 1; (ptr >= s) && isspace(*ptr); --ptr);
ptr[1] = '\0';
return s;
}
Run Code Online (Sandbox Code Playgroud)
它快速可靠 - 为我服务多年。
| 归档时间: |
|
| 查看次数: |
96026 次 |
| 最近记录: |