Created
January 26, 2011 15:07
-
-
Save adamjforster/796797 to your computer and use it in GitHub Desktop.
Truncate a string at the nearest delimiter to a certain length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def truncate_on(string, length, delimiter=' '): | |
"""Truncate a string at the nearest delimiter to a certain length.""" | |
length = int(length) | |
if len(string) > length: | |
index = string.rfind(delimiter, 0, length) | |
if index == -1: | |
string = string[:50] | |
else: | |
string = string[:index] | |
return string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment