Cut the string to be <= 80 characters AND must keep the words without cutting them

前端 未结 4 1595
春和景丽
春和景丽 2021-01-24 07:22

I am new to C#, but I have a requirement to cut the strings to be <= 80 characters AND they must keep the words integrity (without cutting them)

Examples

4条回答
  •  失恋的感觉
    2021-01-24 07:33

    This isn't using regex but this is how I would do it:

    Use String.LastIndexOf to get the last space before the 81st char.
    If the 81th char is a space then take it until 80.
    if it returns a number > -1 cut it off there.
    If it's -1 you-have-a-really-long-word-or-someone-messing-with-the-system so you do wathever you like.

提交回复
热议问题