从字符串中删除前导和尾随空格 [英] Removing leading and trailing spaces from a string

查看:324
本文介绍了从字符串中删除前导和尾随空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C ++中的字符串对象中删除空格。

例如,如何从下面的字符串对象中删除前导和尾随的空格。

How to remove spaces from a string object in C++.
For example, how to remove leading and trailing spaces from the below string object.

//Original string: "         This is a sample string                    "
//Desired string: "This is a sample string"

我知道的字符串类不提供任何方法来删除和尾随空格。

The string class, as far as I know, doesn't provide any methods to remove leading and trailing spaces.

要添加问题,如何扩展此格式以处理字符串的字之间的额外空格。例如,

To add to the problem, how to extend this formatting to process extra spaces between words of the string. For example,

// Original string: "          This       is         a sample   string    " 
// Desired string:  "This is a sample string"

使用解决方案中提到的字符串方法,我可以想到做这些操作

Using the string methods mentioned in the solution, I can think of doing these operations in two steps.


  1. 删除前导和尾随的空格。

  2. 使用 find_first_of,find_last_of


推荐答案

,find_first_not_of,find_last_not_of和substr

这叫做修剪。您要使用 find_first_not_of 获取第一个非空格字符的索引,然后使用 find_last_not_of 获取索引结束不是空格。使用这些,使用 substr 获得没有周围空白的子字符串。

It's called trimming. You want to use find_first_not_of to get the index of the first non-whitespace character, then find_last_not_of to get the index from the end that isn't whitespace. With these, use substr to get the sub-string with no surrounding whitespace.

我不知道这个术语,但我猜想一些沿着减少的线,所以这就是我所说的。 :)(注意,为了灵活性,我将白色空间改为一个参数)

In response to your edit, I don't know the term but I'd guess something along the lines of "reduce", so that's what I called it. :) (Note, I've changed the white-space to be a parameter, for flexibility)

#include <iostream>
#include <string>

std::string trim(const std::string& str,
                 const std::string& whitespace = " \t")
{
    const auto strBegin = str.find_first_not_of(whitespace);
    if (strBegin == std::string::npos)
        return ""; // no content

    const auto strEnd = str.find_last_not_of(whitespace);
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}

std::string reduce(const std::string& str,
                   const std::string& fill = " ",
                   const std::string& whitespace = " \t")
{
    // trim first
    auto result = trim(str, whitespace);

    // replace sub ranges
    auto beginSpace = result.find_first_of(whitespace);
    while (beginSpace != std::string::npos)
    {
        const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
        const auto range = endSpace - beginSpace;

        result.replace(beginSpace, range, fill);

        const auto newStart = beginSpace + fill.length();
        beginSpace = result.find_first_of(whitespace, newStart);
    }

    return result;
}

int main(void)
{
    const std::string foo = "    too much\t   \tspace\t\t\t  ";
    const std::string bar = "one\ntwo";

    std::cout << "[" << trim(foo) << "]" << std::endl;
    std::cout << "[" << reduce(foo) << "]" << std::endl;
    std::cout << "[" << reduce(foo, "-") << "]" << std::endl;

    std::cout << "[" << trim(bar) << "]" << std::endl;
}

结果:


[too much               space]  
[too much space]  
[too-much-space]  
[one  
two]  

虽然如果您可以 Boost ,我会推荐它。

Though if you can Boost, I'd recommend it.

这篇关于从字符串中删除前导和尾随空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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