Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
re solution in Clear category for Cut Sentence by yoichi
import re
def cut_sentence(line, length):
'''Cut a given sentence, so it becomes shorter than or equal to a given length.'''
if len(line) <= length:
return line
m = re.match(r'.{0,' + str(length) + '}\s', line)
return (m.group(0)[:-1] if m else '') + '...'
Oct. 3, 2017
Comments: