Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

1. 关键词

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

2. strutil.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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. 测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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. 运行结果

1
2
3
4
5
6
7
---------------------------------------------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。

推荐阅读
C++ 字符串处理5-手机号邮箱如何脱敏处理 C++ 字符串处理5-手机号邮箱如何脱敏处理 C++ 字符串处理1-将字符串转成大写或小写 C++ 字符串处理1-将字符串转成大写或小写 C++ 字符串处理4-根据指定的分隔符将字符串分割为多个子串&根据指定的分隔符将多个子串连接成一个字符串 C++ 字符串处理4-根据指定的分隔符将字符串分割为多个子串&根据指定的分隔符将多个子串连接成一个字符串

评论