c++ - 如何修剪 std::string?

标签 c++ trim stdstring

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

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

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

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

最佳答案

编辑 自 c++17 起,标准库的某些部分已被删除。幸运的是,从 c++11 开始,我们有了 lambda,这是一个更好的解决方案。

#include <algorithm> 
#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(), [](unsigned char ch) {
        return !std::isspace(ch);
    }));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
        return !std::isspace(ch);
    }).base(), s.end());
}

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

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

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

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

感谢https://stackoverflow.com/a/44973498/524503提出现代解决方案。

原答案:

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

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

它们相当不言自明并且工作得很好。

编辑:顺便说一句,我有 std::ptr_fun 来帮助消除 std::isspace 的歧义,因为实际上还有第二个定义支持语言环境。这本可以是相同的类型转换,但我更喜欢这个。

编辑:解决一些关于通过引用接受参数、修改和返回参数的评论。我同意。我可能更喜欢的实现是两组函数,一组用于就地,另一组用于复制。一组更好的例子是:

#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) {
    rtrim(s);
    ltrim(s);
}

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

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

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

我保留上面的原始答案,但为了上下文和为了保持高投票答案仍然可用。

关于c++ - 如何修剪 std::string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25384690/

相关文章:

c++ - 在 Ubuntu 中通过终端运行 C++ 时遇到问题

wordpress - 我想在使用 wp_trim_words 时允许 html 标签

vbscript - 修剪前导空格,包括制表符

c++ - 如何在 C++ 中将 int 附加到字符串?

c++ - 为什么在离开作用域时指向字符串文字的外部指针会丢失? (C++)

c++ - 错误 : no match for call to '(const std::basic_string<char>) ()'

c++ 返回不同的结果

c++11 lambda 表达式问题(vs2012 update 1)

c# - 为什么 String.Trim 不修剪分号?

c++ - QDomDocument - 如何从 QString 创建?