【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;
}
测试结果
转载于:https://www.cnblogs.com/QG-whz/p/4802273.html
【C++实现python字符串函数库】strip、lstrip、rstrip方法相关推荐
- python rstrip函数_【C++实现python字符串函数库】strip、lstrip、rstrip方法
[C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...
- python中的rstrip函数_Python strip() lstrip() rstrip() 函数 去除空格
函数:strip() lstrip() rstrip() 作用:去除字符串中的空格或指定字符 一.默认用法:去除空格 str.strip() : 去除字符串两边的空格 str.lstrip() ...
- c int转字符串_【C++实现python字符串函数库】字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- Python零碎知识(1):strip lstrip rstrip使用方法
一.原理介绍: Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符.这三个函数都可传入一个参数,指定要去除的首尾字符.需要注意的是 ...
- python 字符串函数 center_python函数及字符串
今天继续函数的知识总结,还有一些关于字符串的内容. 1.递归函数:函数自己调用自己 使用递归函数条件①必须留出口(函数调用必须有退出)②自己调用自己 def print_num(num): print ...
- 对比python字符串函数,学习pandas的str矢量化字符串函数
1.概述 python字符串应该是python里面最重要的数据类型了,因此学会怎么处理各种各样的字符串,显得尤为重要. 我们不仅要学会怎么处理单个字符串,这个就需要学习"python字符串函 ...
- Python字符串函数的使用
Python字符串函数的使用 字符串函数语法及功能介绍 input() 接受用户输入的信息,返回字符串 无论用户输入什么类型的数据,最终都自动转换成字符串 #input()的使用 #语法:input( ...
- python 字符串函数_Python字符串函数
python 字符串函数 Python provides a lot of built-in functions to manipulate strings. Python String is imm ...
- python中upper函数有什么用_几个有用的python字符串函数(format,join,split,startwith,endwith,lower,upper)...
你需要知道的python字符串函数 format 字符串的format函数为非字符串对象嵌入字符串提供了一种非常强大的方法.在format方法中,字符串使用{}来代替一系列字符串的参数并规定格式.下面 ...
- python中怎么调用函数_浅谈Python中函数的定义及其调用方法
一.函数的定义及其应用 所谓函数,就是把具有独立功能的代码块组织成为一个小模块,在需要的时候调用函数的使用包含两个步骤 1.定义函数–封装独立的功能 2.调用函数–享受封装的成果 函数的作用:在开发时 ...
最新文章
- java 克隆的作用_关于java中克隆的学习(一)
- es like模糊匹配_es 基于match_phrase/fuzzy的模糊匹配原理及使用
- python输出到文件
- python写界面c这算法_插入算法分别从C,java,python三种语言进行书写
- linux ls命令shell脚本位置,linux - shell脚本到ls并在ls上执行命令结果 - SO中文参考 - www.soinside.com...
- linux的yum命令无法使用在哪里下载_Linux 知识分享:为Linux的cp和mv命令添加进度条...
- IDEA下java的SSM框架搭建
- django中设置url或者models中的slug字段
- android 中的 gridview 的用法
- google 浏览器使用的一些小技巧
- java 输出 new date,new Date() 方法到底是获取什么时间
- Face photo recognition using sketch (人脸画像合成)
- 笔记本方向键和HOME END 键是 同一个按钮,怎么使用?
- English--consonant_爆破音
- 宝塔面板linux账号密码忘记,宝塔面板忘记账号或密码的解决办法
- 华硕fl5600l装固态并重装系统到固态
- Quectel EC200A-CN移植
- 经典趣味数学问题之过河问题
- python随机生成生日测试生日悖论
- 江苏转本计算机一级,江苏“专转本”新规:考生需获计算机一级及以上证书
热门文章
- pyqt5多进程 python_Python 多进程大全
- supervisor常用命令
- 巴基斯坦削减光伏发电上网电价或影响清洁能源发展前景
- centos更新163源并升级内核
- 七牛云存储基于时间戳防盗链算法参考实现
- android:layout_height、android:layout_width、android:height、android:width的关系与区别
- ie6下 jsonp无响应的问题
- BW作为源系统连接时,激活DSO或其他模型时提示8*数据源不存在,无法激活
- Switch View when host XmlFormView in aspx
- 在主函数中输入10个等长的字符串。用另一函数对他们排序。