【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方法的更多相关文章

  1. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  2. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

  3. 【转】Python中string的strip,lstrip,rstrip用法

    Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...

  4. Python strip lstrip rstrip使用方法(字符串处理空格)

    Python strip lstrip rstrip使用方法(字符串处理空格)   strip是trim掉字符串两边的空格.lstrip, trim掉左边的空格rstrip, trim掉右边的空格 s ...

  5. Python误区之strip,lstrip,rstrip

    最近在处理数据的时候,想把一个字符串开头的“)”符号去掉,所以使用targetStr.lstrip(")"),发现在 将处理完的数据插入到数据库时会出现编码报错,于是在网上搜到了这 ...

  6. 【276】◀▶ Python 字符串函数说明

    参考:Python 字符串函数 01   capitalize 把字符串的第一个字符大写,其他字母变小写. 02   center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...

  7. 13-C语言字符串函数库

    目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...

  8. Lua 中的string库(字符串函数库)总结

    (字符串函数库)总结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-11-20我要评论 这篇文章主要介绍了Lua中的string库(字符串函数库)总结,本文讲解了string库 ...

  9. yum安装的时候报错,关于python的函数库

    我在执行yum -y install nc命令的时候出现如下报错 There was a problem importing one of the Python modulesrequired to ...

随机推荐

  1. css3和原生js时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Convert.ToDateTime(值),方法可以把一个值转化成DateTime类型。

    例子:将日历控件的值转化成DateTime类型. DateTime beginDate = Convert.ToDateTime(this.beginCalendar.EditValue);

  3. php进阶函数

    1,对文件的操作,确保多个进程可以同时读写一个文件(flock函数) flock($hamdle,int $operator) operator的取值,LOCK_SH(共享锁定,读取程序),LOCK_ ...

  4. Openjudge 1.13-40 提取数字串按数值排序

    40:提取数字串按数值排序 查看 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个字符串,请将其中的所有数字串提取,并将每个数字串作为整数看待(假设可以用int 表示),按从 ...

  5. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  6. 在GoF设计模式

    在GoF设计模式中,结构型模式有: 1.适配器模式 Adapter   适配器模式是将一个类的接口转换成客户希望的另外一个接口.适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.   ...

  7. SQL Server中的索引结构与疑惑

    说实话我从没有在实际项目中使用过索引,仅知道索引是一个相当重要的技术点,因此我也看了不少文章知道了索引的区别.分类.优缺点以及如何使用索引.但关于索引它最本质的是什么笔者一直没明白,本文是笔者带着这些 ...

  8. JVM内存管理------GC算法精解(五分钟让你彻底明白标记/清除算法)

    相信不少猿友看到标题就认为LZ是标题党了,不过既然您已经被LZ忽悠进来了,那就好好的享受一顿算法大餐吧.不过LZ丑话说前面哦,这篇文章应该能让各位彻底理解标记/清除算法,不过倘若各位猿友不能在五分钟内 ...

  9. 用node.js实现简单的web服务器

    node.js实现web服务器还是比较简单的,我了解node.js是从<node入门>开始的,如果你不了解node.js也可以看看! 我根据那书一步一步的练习完了,也的确大概了解了node ...

  10. Bootstrap系列 -- 7. 列表排版方式

    一. 去点列表 1. 使用class=list-unstyled <ul > <li>无序列表项目</li> <li>无序列表项目</li> ...