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

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

这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值。startswith()函数判断文本的指定范围字符段是否以某个字符开始,endswith()函数判断文本是否以某个字符结束。默认的指定范围为整个字符串:

>>>

>>> a

'abcdefghijklmn'

>>> a.startswith('abc')

True

>>> a.endswith('klmn')

True

>>> a.startswith('bc')

False

>>> a.endswith('nm')

False

>>>

也可以指定一个匹配范围:

>>>

>>> a

'abcdefghijklmn'

>>> a.startswith('cd',2,10)

True

>>>

python字符串范围校准。

在使用字符串函数时,很多时候我们可以使用start与end参数来指定要进行操作的字符串的一个范围。例如在上面的函数中我们就使用到了('cd',2,10)语句,来对字符串a下标从2~10的范围进行匹配操作。

当我们输入的范围不合法时,python是如何处理的呢?例如我们输入了一个负数的start或者输入一个远大于字符串长度的end,python的处理绝不是以字符串开始或结束位置作为标准来校正范围,请看下面这段程序:

>>> a

'abcdefghijklmn'

>>> len(a)

14

>>> a.startswith('ef',-10,10) #实际范围:(-10+14,10)=(4,10)

具体的校准方法,我们可以使用这函数来描述:

void AdjustIndices(int &start, int & end, std::string::size_type len)

{

len =(int)len;

//如果end超出字符串长度

if (end > len)

end = len; //则以字符串长度为准

else if (end < 0)

{//如果end为负数

end += len; //则先加上字符串长度

if (end < 0)//如果还是为负数

end = 0;//则为0

}

//如果start为负数

if (start < 0)

{

//则加上字符串长度,注意不是以0校准

start += len;

if (start < 0)//如果还是负数

start = 0;//才以0校准

}

}

然而在我们的函数库实现中,我们并不打算把范围校准操作作为一个函数。我们将它作为一个宏来处理,原因如下:

操作简单,不会出来宏函数常见的问题,直接的替换足以解决问题。

省去函数调用的花销

多个地方都需要范围校准。

C++实现

范围校准宏

#define ADJUST_INDICES(start, end, len) \

if (end > len) \

end = len; \

else if (end < 0) { \

end += len; \

if (end < 0) \

end = 0; \

} \

if (start < 0) { \

start += len; \

if (start < 0) \

start = 0; \

}

有上面的解说,这段宏定义应该看得懂。

_string_tailmatch函数

//匹配函数:endswith与startwith的内部调用函数

int _string_tailmatch(const std::string&self, const std::string&substr, int start, int end, int direction)

{

int selflen = (int)self.size();

int slen = (int)substr.size();

const char* str = self.c_str();

const char* sub = substr.c_str();

//对输入的范围进行校准

ADJUST_INDICES(start, end, selflen);

//字符串头部匹配(即startswith)

if (direction < 0)

{

if (start + slen>selflen)

return 0;

}

//字符串尾部匹配(即endswith)

else

{

if (end - startselflen)

return 0;

if (end - slen > start)

start = end - slen;

}

if (end - start >= slen)

//mcmcmp函数用于比较buf1与buf2的前n个字节

return !std::memcmp(str + start, sub, slen);

return 0;

}

endswith函数

bool endswith(const std::string&str, const std::string&suffix, int start = 0, int end = MAX_32BIT_INT)

{

//调用_string_tailmatch函数,参数+1表示字符串尾部匹配

int result = _string_tailmatch(str, suffix, start, end, +1);

return static_cast(result);

}

startswith函数

bool startswith(const std::string&str, const std::string&suffix, int start = 0, int end = MAX_32BIT_INT)

{

//调用_string_tailmatch函数,参数-1表示字符串头部匹配

int result = _string_tailmatch(str, suffix, start, end, -1);

return static_cast(result);

}

测试

string str = "abcdefghijklmn";

string temp1 = "ab";

cout << startswith(str, temp1)<

string temp2 = "mn";

cout << endswith(str, temp2) << endl;

string temp3 = "ef";

cout << startswith(str, temp3, 4, 10)<

string temp4 = "qq";

cout << startswith(str, temp3, 0, 100) << endl;

测试结果

python实现 字符串匹配函数

通配符是 shell 命令中的重要功能,? 表示匹配任意 1 个字符,*表示匹配 0 个或多个字符.请使用你熟悉的编程语言实现一个字符串匹配函数,支持 ? 和 * 通配符.如 "a?cd*d ...

numpy函数库中一些常用函数的记录

##numpy函数库中一些常用函数的记录 最近才开始接触Python,python中为我们提供了大量的库,不太熟悉,因此在的学习中,对遇到的一些函数的用法进行记录. (1) ...

C语言字符串匹配函数

C语言字符串匹配函数,保存有需要时可以用: #include #include #include # ...

Python中字符串String的基本内置函数与过滤字符模块函数的基本用法

Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...

Python字符串常用方法(二)

二.字符串的操作常用方法 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1. string. lower() :转小写 2. string. upper() :转大写 3. string. ...

Python字符串中删除特定字符

分析 在Python中,字符串是不可变的.所以无法直接删除字符串之间的特定字符. 所以想对字符串中字符进行操作的时候,需要将字符串转变为列表,列表是可变的,这样就可以实现对字符串中特定字符的操作. 1 ...

python字符串格式化方法&percnt;s和format函数

1.%s方法 一个例子 print("my name is %s and i am %d years old" %("xiaoming",18) 输出结果:my ...

BZOJ4259:残缺的字符串(FFT与字符串匹配)

很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. 你想对这两 ...

Python中字符串匹配函数startswith&lpar;&rpar;函数

1.函数用途含义 Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在指定范围内 ...

随机推荐

【NET MVC】View

通过阅读一些书籍,结合源代码,稍微深入的学习了Asp.Net MVC中的视图View 任何类型的响应都可以利用当前HttpResponse来响应,MVC可以通过Controller的Response属 ...

Java面试(2)-- Java算数表达式

class Demo02{ public static void main(String[] args){ //算数运算符 +,-,*,/,%,++,-- //例1 int a = 1; int b ...

咱们来聊聊JS中的异步,以及如何异步,菜鸟版

为什么需要异步?why?来看一段代码. 问题1: for(var i=0;i<100000;i++){ } alert('hello world!!!'); 这段代码的意思是执行100...次后 ...

Unity 3D学习之 Prime31 Game Center插件用法

http://momowing.diandian.com/post/2012-11-08/40041806328 It's my life~: 为app 连入Game Center 功能而困扰的朋友们 ...

ios开发逆向传值的几种方法整理

第一种:代理传值 第二个控制器: @protocol WJSecondViewControllerDelegate - (void)changeText:(NSStr ...

Android Fragment 基本介绍&lbrack;转&rsqb;

Fragment Android是在Android 3.0 (API level 11)开始引入Fragment的. 可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的 ...

Swipecards

https://github.com/Diolor/Swipecards https://github.com/kikoso/Swipeable-Cards

假防病毒软件从电脑移植到了 Android 平台

以前有位女研究生点击网络钓鱼的链接.随即出现实时扫毒画面的方式,接着呈现了扫毒结果,跑出十余笔病毒数据,记录了被感染的计算机的具体位置,并提示她必须更新防病毒软件,而她在付费两千元后收到"防 ...

PyCharm 2018 永久激活

PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制. ...

Pyhon学习笔记-基础3

文件操作 1.基本操作 f = open("filename","r",encoding="utf-8") #打开文件,以r模式,字符编码模 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值