From 363999badfce6dbe9d05574a3999cba246fbe789 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Fri, 31 Jan 2014 10:07:05 -0500 Subject: [PATCH] * Added str_lstrip, str_rstrip functions for a Bourne-like substring stripping. --- text_tools.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/text_tools.py b/text_tools.py index 71f9aed..7f99254 100644 --- a/text_tools.py +++ b/text_tools.py @@ -107,6 +107,23 @@ def str_trunc_end(S, L): else: return S +def str_lstrip(S, substr): + """Strips a prefix from S if it matches the string in `substr'. + """ + # This is akin to ${VAR#$substr} in Bourne-shell dialect. + if len(substr) > 0 and S.startswith(substr): + return S[len(substr):] + else: + return S + +def str_rstrip(S, substr): + """Strips a suffix from S if it matches the string in `substr'. + """ + # This is akin to ${VAR%$substr} in Bourne-shell dialect. + if len(substr) > 0 and S.endswith(substr): + return S[:-len(substr)] + else: + return S def str_save_to_file(filename, s1, *more_str, **opts): """Save one or more string (or iterables) to a file with a given file.