【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 ...
随机推荐
- SQLite 增、删、改、查
1.iOS中实现SQLite的增.删.改.查 http://www.jianshu.com/p/0b9b78e704a4. 2.用数据库实现收藏功能 http://www.jianshu.com/p ...
- 利用substring()方法,把一个表的不同分级所对应的字段名取出来。
实例:现在有一个物料分类的表.知道表的第四级的值,要取前面的2,3级值. 例如,如图所示: 可以通过取前面几个字段的值,得到对应级别的值.利用substring(),但是因为要写在一句话里. 因此可以 ...
- JS 中如何判断字符串类型的数字
function isNumberStr(str){ var n = Number(str); return !isNaN(n); } console.log(isNumberStr('37')); ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- linux ISO/IMG make
sudo dd if=/PATH/*.ISO of=/dev/sdb 1.制作启动U盘需要sdb,不能sdb1,否则会提示isolinux.bin文件丢失 2.TF卡,设置sdb1?忘了 /* sy ...
- scala 学习笔记(01) 函数定义、分支、循环、异常处理、递归
package yjmyzz import scala.io.StdIn object ScalaApp { def main(args: Array[String]) { println(" ...
- 解决 docker on windows下网络不通
问题:公司有一台闭置的windows服务器,于是想利用起来,但是在启动容器后始终无法通信成功. 研究: 1. 发现安装包中包含virtualbox, 于是怀疑windows下的docker是在virt ...
- python数字图像处理(18):高级形态学处理
形态学处理,除了最基本的膨胀.腐蚀.开/闭运算.黑/白帽处理外,还有一些更高级的运用,如凸包,连通区域标记,删除小块区域等. 1.凸包 凸包是指一个凸多边形,这个凸多边形将图片中所有的白色像素点都包含 ...
- [BZOJ1061][Noi 2008]志愿者招募(网络流)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1061 分析: 神题不解释,只能欣赏:https://www.byvoid.com/bl ...
- 安装Ubuntu之后
一.Ubuntu is better than fedora I used to use Utuntu 14.04,it's a LTS(long term support) edition. I d ...