c++去掉字符串首尾不可见字符

//Author:Donny
//This is ...
#include<bits/stdc++.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>

版本一:

std::string& trim(std::string &s) {$
    if(s.empty()) {$
        return s;$
    }$
    string character = "";$
    for(int i = 0; i < 33; i++) {$
        character += char(i);$
    }$
    character += char(127);$
    s.erase(0,s.find_first_not_of(character));$
    s.erase(s.find_last_not_of(character) + 1);$
    return s;$

}

版本二:

std::string& trim(std::string &s)
{
    if(s.empty()) {
        return s;
    }
    s.erase(0,s.find_first_not_of("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\
                                  \x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\1F\x20\x7F\x00"));
    s.erase(s.find_last_not_of("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\
                               \x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\1F\x20\x7F\x00") + 1);
    return s;
}

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

string str2 = "abc12123xyz-abc12123xyz\n";

    trim(str2);

return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值