diff --git a/src/imcflibs/strtools.py b/src/imcflibs/strtools.py index 49db1d54b98b7300d8df6aeec130980981a021e8..2f1c6269ab20d4f050c9e9f960116e085b658328 100644 --- a/src/imcflibs/strtools.py +++ b/src/imcflibs/strtools.py @@ -76,3 +76,24 @@ def flatten(lst): for line in lst: flat += line return flat + + +def strip_prefix(string, prefix): + """Remove a given prefix from a string. + + Parameters + ---------- + string : str + The original string from which the prefix should be removed. + prefix : str + The prefix to be removed. + + Returns + ------- + str + The original string without the given prefix. In case the original + string doesn't start with the prefix, it is returned unchanged. + """ + if string.startswith(prefix): + string = string[len(prefix):] + return string diff --git a/tests/test_strtools.py b/tests/test_strtools.py index c49a762a7ed288829b87e86a702e1d4b8671ae6e..823b56156b054ab41af0b3644174f3e888630f58 100644 --- a/tests/test_strtools.py +++ b/tests/test_strtools.py @@ -7,6 +7,7 @@ import os from imcflibs.strtools import _is_string_like from imcflibs.strtools import filename from imcflibs.strtools import flatten +from imcflibs.strtools import strip_prefix __author__ = "Niko Ehrenfeuchter" __copyright__ = "Niko Ehrenfeuchter" @@ -29,4 +30,9 @@ def test_filename_from_handle(tmpdir): def test_flatten(): - assert flatten(('foo', 'bar')) == 'foobar' \ No newline at end of file + assert flatten(('foo', 'bar')) == 'foobar' + + +def test_strip_prefix(): + assert strip_prefix('foobar', 'foo') == 'bar' + assert strip_prefix('foobar', 'bar') == 'foobar' \ No newline at end of file