python rstrip函数_【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;
}
测试结果
python rstrip函数_【C++实现python字符串函数库】strip、lstrip、rstrip方法相关推荐
- sql字符串函数_另一堆SQL字符串函数
sql字符串函数 This article is a supplement to the previously published article, An overview of SQL String ...
- c连接mysql数据库字符串函数_在mysql数据库—— 字符串函数的运用
在mysql数据库中函数的使用 字符函数: 函数名称 描述 concat 字符连接 Concat_ws 使用指定的分隔符进行字符连接 format 数字格式化(千分化) Lower 转换成小写字母 u ...
- python中的rstrip函数_Python strip() lstrip() rstrip() 函数 去除空格
函数:strip() lstrip() rstrip() 作用:去除字符串中的空格或指定字符 一.默认用法:去除空格 str.strip() : 去除字符串两边的空格 str.lstrip() ...
- python len函数_知识清单Python必备的69个函数,你掌握了吗?
本文纲要 Python 作为一门高级编程语言,为我们提供了许多方便易用的内置函数,节省了不少开发应用的时间.目前,Python 3.7 共有 69 个内置函数,一些是我们耳熟能详的函数,另一些却不是很 ...
- python zip函数_相当于Python的zip函数
下面是一个更时髦的ECMAScript 6版本:zip= rows=>rows[0].map((_,c)=>rows.map(row=>row[c])) 插图等价物到Python{z ...
- 学python处理数据结构_从零开始学Python - 第009课:常用数据结构之字符串
第二次世界大战促使了现代电子计算机的诞生,世界上的第一台通用电子计算机叫ENIAC(电子数值积分计算机),诞生于美国的宾夕法尼亚大学,占地167平米,重量27吨,每秒钟大约能够完成约5000次浮点运算 ...
- append函数_高质量python代码:考虑用生成器来改写直接返回列表的函数
写在前面:内容参照自<Effective Python>,其实你完全可以直接去看书,什么?你不想自己看书,那么你也可以关注我,我会不定期从书中挑出常用到的有效方法分享出来,这样你就可以一边 ...
- python sorted下标_全!Python基础之原生数据类型、判断和循环、函数和文件操作合集...
长文预警! Python基础系列会将基础内容大致分为三到五个板块,每块着重讲一方面,知识不会很难,主要是以小例子的形式解读,如果你已经入门Python,希望可以帮你温习一下:如果你想入门Python, ...
- python中怎么调用函数_浅谈Python中函数的定义及其调用方法
一.函数的定义及其应用 所谓函数,就是把具有独立功能的代码块组织成为一个小模块,在需要的时候调用函数的使用包含两个步骤 1.定义函数–封装独立的功能 2.调用函数–享受封装的成果 函数的作用:在开发时 ...
- [转载] python中union函数_如何掌握Python union()方法及怎么用?
参考链接: Python set集合 intersection() 不断学习python的过程里,总能遇到各种形形色色的函数或者方法,本章给大家带来python union的用法,具体内容如下: un ...
最新文章
- python第五单元答案_中国大学MOOC第五单元测试答案_数据结构与算法Python版慕课答案在哪里可以看...
- 自定义checkbox样式
- 测试AtomicInteger与普通int值在多线程下的递增操作
- #iOS问题记录# 关于UITableViewcel的分割线去掉问题
- 类ThreadLocal的使用与源码分析
- python—tf.keras.backend.clear_session()
- iOS开发之iOS11频繁刷新TableView向下偏移的问题
- SDM连接小凡的模拟路由器并配置图文并貌详细解说
- vue路由 Maximum call stack size exceeded
- 微信小程序订阅消息 微信公众号模板消息
- 【Love2d从青铜到王者】第十六篇:Love2d之动画(Animation)
- Android应用开发——记事本
- 如何在 Mac 上的邮件中添加签名来个性化电子邮件?
- 传销——从数学游戏到经济邪教
- Android图像适配
- [附源码]Java计算机毕业设计SSM电脑配件仓储后台管理系统
- 在已安装win10环境中利用EasyBCD引导安装Ubuntu16.04-小白补充
- 内存模块与计算机兼容,内存条不兼容的解决办法【图文教程】
- 超市库存管理java sql_基于JAVA的超市管理(商店库存)系统的设计与实现(Eclipse,SQLServer)...
- 关于MVVM与MVI