summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2002-04-13 00:56:08 (GMT)
committerGuido van Rossum <guido@python.org>2002-04-13 00:56:08 (GMT)
commit018b0eb0f50a6995922402d87cf9906b62a50252 (patch)
tree097e9c9b588ea0ce558fde65dbfa7381ab4b2259 /Lib
parent9344b148282b3a40c44102018e8eac86d6613c51 (diff)
downloadcpython-018b0eb0f50a6995922402d87cf9906b62a50252.zip
cpython-018b0eb0f50a6995922402d87cf9906b62a50252.tar.gz
cpython-018b0eb0f50a6995922402d87cf9906b62a50252.tar.bz2
Partially implement SF feature request 444708.
Add optional arg to string methods strip(), lstrip(), rstrip(). The optional arg specifies characters to delete. Also for UserString. Still to do: - Misc/NEWS - LaTeX docs (I did the docstrings though) - Unicode methods, and Unicode support in the string methods.
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/UserString.py6
-rw-r--r--Lib/test/string_tests.py12
2 files changed, 15 insertions, 3 deletions
diff --git a/Lib/UserString.py b/Lib/UserString.py
index 45cdeb5..f4f5cab 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -108,7 +108,7 @@ class UserString:
def join(self, seq): return self.data.join(seq)
def ljust(self, width): return self.__class__(self.data.ljust(width))
def lower(self): return self.__class__(self.data.lower())
- def lstrip(self): return self.__class__(self.data.lstrip())
+ def lstrip(self, sep=None): return self.__class__(self.data.lstrip(sep))
def replace(self, old, new, maxsplit=-1):
return self.__class__(self.data.replace(old, new, maxsplit))
def rfind(self, sub, start=0, end=sys.maxint):
@@ -116,13 +116,13 @@ class UserString:
def rindex(self, sub, start=0, end=sys.maxint):
return self.data.rindex(sub, start, end)
def rjust(self, width): return self.__class__(self.data.rjust(width))
- def rstrip(self): return self.__class__(self.data.rstrip())
+ def rstrip(self, sep=None): return self.__class__(self.data.rstrip(sep))
def split(self, sep=None, maxsplit=-1):
return self.data.split(sep, maxsplit)
def splitlines(self, keepends=0): return self.data.splitlines(keepends)
def startswith(self, prefix, start=0, end=sys.maxint):
return self.data.startswith(prefix, start, end)
- def strip(self): return self.__class__(self.data.strip())
+ def strip(self, sep=None): return self.__class__(self.data.strip(sep))
def swapcase(self): return self.__class__(self.data.swapcase())
def title(self): return self.__class__(self.data.title())
def translate(self, *args):
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 4b2f0e3..5c8dd93 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -163,6 +163,18 @@ def run_method_tests(test):
test('rstrip', ' hello ', ' hello')
test('strip', 'hello', 'hello')
+ # strip/lstrip/rstrip with None arg
+ test('strip', ' hello ', 'hello', None)
+ test('lstrip', ' hello ', 'hello ', None)
+ test('rstrip', ' hello ', ' hello', None)
+ test('strip', 'hello', 'hello', None)
+
+ # strip/lstrip/rstrip with real arg
+ test('strip', 'xyzzyhelloxyzzy', 'hello', 'xyz')
+ test('lstrip', 'xyzzyhelloxyzzy', 'helloxyzzy', 'xyz')
+ test('rstrip', 'xyzzyhelloxyzzy', 'xyzzyhello', 'xyz')
+ test('strip', 'hello', 'hello', 'xyz')
+
test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS')
test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def')