什么是修剪std :: string的最好的方法? [英] What's the best way to trim std::string?

查看:328
本文介绍了什么是修剪std :: string的最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用以下代码在我的程序中右键修剪所有 std :: strings

  std :: string s; 
s.erase(s.find_last_not_of(\\\
\r\t)+ 1);

它工作正常,但我想知道是否有一些终止的情况可能会失败? p>

当然,欢迎使用优雅的替代方案和左修剪解决方案。

解决方案

我倾向于使用这3个中的一个来满足我的修剪需求:

  #include< algorithm& 
#include< functional>
#include< cctype>
#include< locale>

//从开始修剪
static inline std :: string& ltrim(std :: string& s){
s.erase(s.begin std :: find_if(s.begin(),s.end(),
std :: not1(std :: ptr_fun< int,int>(std :: isspace)
return s;
}

//从结尾修剪
static inline std :: string& rtrim(std :: string& s){
s.erase :: find_if(s.rbegin(),s.rend(),
std :: not1(std :: ptr_fun< int,int>(std :: isspace)))。base(),s.end ());
return s;
}

//从两端修剪
static inline std :: string& trim(std :: string& s){
return ltrim (s))。
}

他们相当自我解释,工作得很好。



EDIT :Btw,我有 std :: ptr_fun ,以帮助消除 std :: isspace ,因为实际上有一个支持语言环境的第二个定义。



EDIT :回答一些关于接受参数的注释通过引用,修改和返回它。我同意。我可能更喜欢的一个实现是两组函数,一个用于就地和一个复制。一组更好的例子是:

  #include< algorithm> 
#include< functional>
#include< cctype>
#include< locale>

//从开始修剪(原位)
static inline void ltrim(std :: string& s){
s.erase(s.begin(),std :: find_if(s.begin(),s.end(),
std :: not1(std :: ptr_fun< int,int>(std :: isspace)
}

//从结尾修剪(原位)
static inline void rtrim(std :: string& s){
s.erase(std: :find_if(s.rbegin(),s.rend(),
std :: not1(std :: ptr_fun< int,int>(std :: isspace)))。base(),s.end ));
}

//从两端修剪(原位)
static inline void trim(std :: string& s){
ltrim
rtrim(s);
}

//从开始修剪(复制)
static inline std :: string ltrimmed(std :: string s){
ltrim
return s;
}

//从结尾修剪(复制)
static inline std :: string rtrimmed(std :: string s){
rtrim
return s;
}

//从两端修剪(复制)
static inline std :: string trimmed(std :: string s){
trim
return s;
}



我保持上面的原始答案,高投票答案仍然可用。


I'm currently using the following code to right-trim all the std::strings in my programs:

std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+1);

It works fine, but I wonder if there are some end-cases where it might fail?

Of course, answers with elegant alternatives and also left-trim solution are welcome.

解决方案

I tend to use one of these 3 for my trimming needs:

#include <algorithm> 
#include <functional> 
#include <cctype>
#include <locale>

// trim from start
static inline std::string &ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
            std::not1(std::ptr_fun<int, int>(std::isspace))));
    return s;
}

// trim from end
static inline std::string &rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(),
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
    return s;
}

// trim from both ends
static inline std::string &trim(std::string &s) {
    return ltrim(rtrim(s));
}

They are fairly self explanatory and work very well.

EDIT: Btw, I have std::ptr_fun in there to help disambiguate std::isspace because there is actually a second definition which supports locales. This could have been a cast just the same, but I tend to like this better.

EDIT: To address some comments about accepting a parameter by reference, modifying and returning it. I Agree. An implementation that I would likely prefer would be two sets of functions, one for in place and one which makes a copy. A better set of examples would be:

#include <algorithm> 
#include <functional> 
#include <cctype>
#include <locale>

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
            std::not1(std::ptr_fun<int, int>(std::isspace))));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(),
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrimmed(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrimmed(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trimmed(std::string s) {
    trim(s);
    return s;
}

I am keeping the original answer above though for context and in the interest of keeping the high voted answer still available.

这篇关于什么是修剪std :: string的最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆