mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bpo-39939: Add str.removeprefix and str.removesuffix (GH-18939)
Added str.removeprefix and str.removesuffix methods and corresponding bytes, bytearray, and collections.UserString methods to remove affixes from a string if present. See PEP 616 for a full description.
This commit is contained in:
parent
39652cd8bd
commit
a81849b031
13 changed files with 597 additions and 6 deletions
|
@ -1239,6 +1239,14 @@ class UserString(_collections_abc.Sequence):
|
|||
if isinstance(sub, UserString):
|
||||
sub = sub.data
|
||||
return self.data.count(sub, start, end)
|
||||
def removeprefix(self, prefix, /):
|
||||
if isinstance(prefix, UserString):
|
||||
prefix = prefix.data
|
||||
return self.__class__(self.data.removeprefix(prefix))
|
||||
def removesuffix(self, suffix, /):
|
||||
if isinstance(suffix, UserString):
|
||||
suffix = suffix.data
|
||||
return self.__class__(self.data.removesuffix(suffix))
|
||||
def encode(self, encoding='utf-8', errors='strict'):
|
||||
encoding = 'utf-8' if encoding is None else encoding
|
||||
errors = 'strict' if errors is None else errors
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue