diff options
author | Guido van Rossum <guido@python.org> | 1997-03-25 16:50:31 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-03-25 16:50:31 (GMT) |
commit | 1eb9a81eb92dd5d133c8584bad8aecd5b8f0ccd1 (patch) | |
tree | 60d329122763365bb9e8d5de03caacc3fed7e3e1 | |
parent | c8a80cdbad5d925400615e09c4bf720de5976181 (diff) | |
download | cpython-1eb9a81eb92dd5d133c8584bad8aecd5b8f0ccd1.zip cpython-1eb9a81eb92dd5d133c8584bad8aecd5b8f0ccd1.tar.gz cpython-1eb9a81eb92dd5d133c8584bad8aecd5b8f0ccd1.tar.bz2 |
Added new functions replace() and replace1().
-rw-r--r-- | Lib/string.py | 15 | ||||
-rw-r--r-- | Lib/stringold.py | 15 |
2 files changed, 30 insertions, 0 deletions
diff --git a/Lib/string.py b/Lib/string.py index d3ab88f..8207a8e 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -320,6 +320,21 @@ def maketrans(fromstr, tostr): L[fromstr[i]] = tostr[i] return joinfields(L, "") +# Substring replacement (global) +def replace(old, new, str): + return joinfields(splitfields(str, old), new) + +# Substring replacement (1st substring only) +def replace1(old, new, str, i=0, last=None): + if last is None: + i = find(str, old, i) + else: + i = find(str, old, i, last) + if i >= 0: + str = str[:i] + new + str[i+len(old):] + return str + + # Try importing optional built-in module "strop" -- if it exists, # it redefines some string operations that are 100-1000 times faster. # It also defines values for whitespace, lowercase and uppercase diff --git a/Lib/stringold.py b/Lib/stringold.py index d3ab88f..8207a8e 100644 --- a/Lib/stringold.py +++ b/Lib/stringold.py @@ -320,6 +320,21 @@ def maketrans(fromstr, tostr): L[fromstr[i]] = tostr[i] return joinfields(L, "") +# Substring replacement (global) +def replace(old, new, str): + return joinfields(splitfields(str, old), new) + +# Substring replacement (1st substring only) +def replace1(old, new, str, i=0, last=None): + if last is None: + i = find(str, old, i) + else: + i = find(str, old, i, last) + if i >= 0: + str = str[:i] + new + str[i+len(old):] + return str + + # Try importing optional built-in module "strop" -- if it exists, # it redefines some string operations that are 100-1000 times faster. # It also defines values for whitespace, lowercase and uppercase |