]> granicus.if.org Git - python/commitdiff
Added new functions replace() and replace1().
authorGuido van Rossum <guido@python.org>
Tue, 25 Mar 1997 16:50:31 +0000 (16:50 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 25 Mar 1997 16:50:31 +0000 (16:50 +0000)
Lib/string.py
Lib/stringold.py

index d3ab88f6f95709471e3459e54bcb09ea031c9e76..8207a8ea97f6d7dd841427d35727c6e0d61c6fee 100644 (file)
@@ -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
index d3ab88f6f95709471e3459e54bcb09ea031c9e76..8207a8ea97f6d7dd841427d35727c6e0d61c6fee 100644 (file)
@@ -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