[Python-ideas] string codes & substring equality

MRAB python at mrabarnett.plus.com
Wed Nov 27 18:59:01 CET 2013


On 27/11/2013 14:32, spir wrote:
[snip]

> A second need is checking substring equality against constant substrings of
> arbitrary sizes. This is similar to startswith & endswith, except at any code
> index in the source string; a generalisation. In C implementation, it would
> probably delegate to memcomp, with a start pointer set to p_source+i. On the
> Python side, it may be a string method like sub_equals(self, substr, i). Choose
> you preferred name ;-). [1] [4]
>
>       if s.sub_equals(substr, i):
>           # match ok
>
> What do you think? (bis)
>
 >>> help(str.startswith)
Help on method_descriptor:

startswith(...)
     S.startswith(prefix[, start[, end]]) -> bool

     Return True if S starts with the specified prefix, False otherwise.
     With optional start, test S beginning at that position.
     With optional end, stop comparing S at that position.
     prefix can also be a tuple of strings to try.

 >>> help(str.endswith)
Help on method_descriptor:

endswith(...)
     S.endswith(suffix[, start[, end]]) -> bool

     Return True if S ends with the specified suffix, False otherwise.
     With optional start, test S beginning at that position.
     With optional end, stop comparing S at that position.
     suffix can also be a tuple of strings to try.



More information about the Python-ideas mailing list