【C++实现python字符串函数库】strip、lstrip、rstrip方法
【C++实现python字符串函数库】strip、lstrip、rstrip方法
这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' ')。
s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符
示例:
>>> s=' abcdefg ' #默认情况下删除空白符
>>> s.strip()
'abcdefg'
>>>
>>>#位于字符串首尾且在删除序列中出现的字符全部被删掉
>>> s = 'and looking down on tomorrow'
>>> s.strip('awon')
'd looking down on tomorr'
>>>
lsprit是只处理字符串的首部(左端),rsprit是只处理字符串的尾部(右端)。
C++实现
宏
#define LEFTSTRIP 0
#define RIGHTSTRIP 1
#define BOTHSTRIP 2
函数
内部调用函数do_strip
std::string do_strip(const std::string &str, int striptype, const std::string&chars)
{
std::string::size_type strlen = str.size();
std::string::size_type charslen = chars.size();
std::string::size_type i, j;
//默认情况下,去除空白符
if (0 == charslen)
{
i = 0;
//去掉左边空白字符
if (striptype != RIGHTSTRIP)
{
while (i < strlen&&::isspace(str[i]))
{
i++;
}
}
j = strlen;
//去掉右边空白字符
if (striptype != LEFTSTRIP)
{
j--;
while (j >= i&&::isspace(str[j]))
{
j--;
}
j++;
}
}
else
{
//把删除序列转为c字符串
const char*sep = chars.c_str();
i = 0;
if (striptype != RIGHTSTRIP)
{
//memchr函数:从sep指向的内存区域的前charslen个字节查找str[i]
while (i < strlen&&memchr(sep, str[i], charslen))
{
i++;
}
}
j = strlen;
if (striptype != LEFTSTRIP)
{
j--;
while (j >= i&&memchr(sep, str[j], charslen))
{
j--;
}
j++;
}
//如果无需要删除的字符
if (0 == i&& j == strlen)
{
return str;
}
else
{
return str.substr(i, j - i);
}
}
}
strip函数
std::string strip( const std::string & str, const std::string & chars=" " )
{
return do_strip( str, BOTHSTRIP, chars );
}
lstrip函数
std::string lstrip( const std::string & str, const std::string & chars=" " )
{
return do_strip( str, LEFTSTRIP, chars );
}
rstrip函数
std::string rstrip( const std::string & str, const std::string & chars=" " )
{
return do_strip( str, RIGHTSTRIP, chars );
}
测试
int main()
{
string str = " abcdefg";
string result;
//不给定删除序列时默认删除空白字符串
result = strip(str);
cout << "默认删除空白符:" << result << endl;
//指定删除序列
result = strip(str, "gf");
cout << "指定删除序列gf:" << result << endl;
str = "abcdefg";
string chars = "abfg";
//只删除左边
result = lstrip(str, chars);
cout << "删除左边" << result << endl;
//只删除右边
result = rstrip(str, chars);
cout << "删除右边" << result << endl;
system("pause");
return 0;
}
测试结果

【C++实现python字符串函数库】strip、lstrip、rstrip方法的更多相关文章
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
[C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...
- 【转】Python中string的strip,lstrip,rstrip用法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...
- Python strip lstrip rstrip使用方法(字符串处理空格)
Python strip lstrip rstrip使用方法(字符串处理空格) strip是trim掉字符串两边的空格.lstrip, trim掉左边的空格rstrip, trim掉右边的空格 s ...
- Python误区之strip,lstrip,rstrip
最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用targetStr.lstrip(")"),发现在 将处理完的数据插入到数据库时会出现编码报错,于是在网上搜到了这 ...
- 【276】◀▶ Python 字符串函数说明
参考:Python 字符串函数 01 capitalize 把字符串的第一个字符大写,其他字母变小写. 02 center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...
- 13-C语言字符串函数库
目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...
- Lua 中的string库(字符串函数库)总结
(字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...
- yum安装的时候报错,关于python的函数库
我在执行yum -y install nc命令的时候出现如下报错 There was a problem importing one of the Python modulesrequired to ...
随机推荐
- 迭代器和for-of循环 顺便带一下Es5中的.map遍历
let set = new Set(); //set方法去除重复的数据 [1, 2, 3, 4, 2, 8, 4].map(function (elem) { set.add(elem); //遍历完 ...
- AR 不同 继承映射的问题总结
在使用AR(Nhibernate) 做ORM时,使用类的继承体系时,它有不同的映射方式,解决的问题不同,带来的问题差异也很大. 1.所有数据 存储在一张表,不同的类使用 DiscriminatorCo ...
- sudo su权限案例
一 控制sudo: 允许执行所有命令,排除某几个命令(带参数) lanny ALL=(ALL) NOPASSWD:ALL, !/bin/su - root, !/usr/sbin/visudo 如果需 ...
- parsing XML document from class path resource
遇到问题:parsing XML document from class path resource [spring/resources] 解决方法:项目properties— source—remo ...
- Android实现滑动刻度尺效果,选择身高体重和生日
刻度尺效果虽然看起来很美,我个人认为很不实用,即使再不实用,也有用的,鉴于群里成员对我的苦苦哀求,我就分享一个他用不到的,横屏滑动刻度尺,因为他需要竖屏的,哈哈…… 最近群里的开发人员咨询怎样实现刻度 ...
- Mysql之case语句(附带实例)
这段时间,做项目做累了,好不容易有点个人的学习时间,利用这个小时,总结一下,最近做统计的时候常用的case语句吧. 结构:case when… then …end 1.判断的同时改变其值 eg: ...
- spring+mybaties+springMvc+slf4j所需jar包
- [转]搞ACM的你伤不起(转自Roba大神)
劳资六年前开始搞ACM啊!!!!!!!!!! 从此踏上了尼玛不归路啊!!!!!!!!!!!! 谁特么跟劳资讲算法是程序设计的核心啊!!!!!! 尼玛除了面试题就没见过用算法的地方啊!!!!!! 谁再跟 ...
- C# Rotating Oval
This program is used to show how to generate an oval. The moon's orbit around the sun is an oval two ...
- DLL丢失修复
DLL丢失修复,简答傻瓜式! DirectX修复工具(DirectX Repair)是一款系统级工具软件,简便易用.本程序为绿色版,无需安装,可直接运行. 本程序的主要功能是检测当前系统的Dir ...