字符串的trim

  • 去除字符串开头和结尾的空格符
static char* Trim(char* str) {
    while (*str) {
        if (!::isspace(*str))
            break; // 如果当前字符非空格,则跳出循环
        ++str;
    }

    if (str[0] == '\0')
        return str;

    char* ret = str;
    while (*str)
        ++str; // 到字符串最后

    for (--str; ::isspace(*str); --str)
        ; // 从字符串结尾开始往前去除空格字符,如果当前字符非空格,则跳出循环

    str[1] = '\0';

    return ret;
}
posted @ 2019-09-16 16:10  情敌贝多芬  阅读(153)  评论(0编辑  收藏  举报