我正在尝试在javascript中创建一个函数,它将一个文本字符串作为参数,然后通过添加\n
到字符串中为我格式化它,这将形成段落。
- 每个段落的长度只能是 32 个字符
- 在进入下一段时,我必须确保单词没有被切成两半”
示例字符串:
"If we listened to our intellect, we'd never have a love affair. We'd never have a friendship. We'd never go into business, because we'd be cynical. Well, that's nonsense. You've got to jump off cliffs all the time and build your wings on the way down."
后格式应该是这样的:
If we listened to our intellect, \n
we'd never have a love affair. \n
We'd never have a friendship.\n
We'd never go into business, \n
because we'd be cynical. Well, \n
that's nonsense. You've got to \n
jump off cliffs all the time and \n
build your wings on the way down"
我不确定我将如何解决这个问题,并且我不希望任何人解决整个解决方案,我只是想要一个起点,我在如何处理空格和单词以及何时将 \n 插入时遇到了困难字符串。