C++ 字符串处理-去除字符串前后的空字符

1. 关键词

C++ 字符串处理 去除字符串前后的空字符 跨平台

2. strutil.h

#include <string>
namespace cutl
{
    /**
     * @brief Remove leading whitespaces from a string.
     *
     * @param str the string to be stripped.
     * @return std::string the stripped string.
     */
    std::string lstrip(const std::string &str);
    /**
     * @brief Remove trailing whitespaces from a string.
     *
     * @param str the string to be stripped.
     * @return std::string the stripped string.
     */
    std::string rstrip(const std::string &str);
    /**
     * @brief Remove leading and trailing whitespaces from a string.
     *
     * @param str the string to be stripped.
     * @return std::string the stripped string.
     */
    std::string strip(const std::string &str);
} // namespace cutl

3. strutil.cpp

#include <cctype>
#include <algorithm>
#include "strutil.h"

namespace cutl
{
    std::string lstrip(const std::string &str)
    {
        if (str.empty())
        {
            return "";
        }

        size_t index = 0;
        for (size_t i = 0; i < str.length(); i++)
        {
            if (!std::isspace(str[i]))
            {
                index = i;
                break;
            }
        }

        return str.substr(index, str.length() - index);
    }

    std::string rstrip(const std::string &str)
    {
        if (str.empty())
        {
            return "";
        }

        size_t index = str.length() - 1;
        for (size_t i = str.length() - 1; i >= 0; i--)
        {
            if (!std::isspace(str[i]))
            {
                index = i;
                break;
            }
        }
        return str.substr(0, index + 1);
    }

    std::string strip(const std::string &str)
    {
        if (str.empty())
        {
            return "";
        }

        size_t index1 = 0;
        for (size_t i = 0; i < str.length(); i++)
        {
            if (!std::isspace(str[i]))
            {
                index1 = i;
                break;
            }
        }
        size_t index2 = str.length() - 1;
        for (size_t i = str.length() - 1; i >= 0; i--)
        {
            if (!std::isspace(str[i]))
            {
                index2 = i;
                break;
            }
        }
        auto len = index2 - index1 + 1;

        return str.substr(index1, len);
    }
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strutil.h"

void TestStrip()
{
    PrintSubTitle("TestStrip");

    std::string text = "  \tThis is a test string. \n ";
    // std::string text = "  \t中国 \n ";
    std::cout << "text: " << text << std::endl;
    std::cout << "trim left text: " << cutl::lstrip(text) << std::endl;
    std::cout << "trim right text: " << cutl::rstrip(text) << std::endl;
    std::cout << "trim text: " << cutl::strip(text) << std::endl;
}

5. 运行结果

---------------------------------------------TestStrip----------------------------------------------
text:   	This is a test string. 
 
trim left text: This is a test string. 
 
trim right text:   	This is a test string.
trim text: This is a test string.

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌尘(MoChen)

爱打赏的人技术成长更开哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值