我试图将一长串文本截断到一定长度,但也希望确保截断的结果在空格处结束.之后我还要附加一个省略号.
例如:
"This is a very long string that has more characters than I want in it."
Run Code Online (Sandbox Code Playgroud)
成为这个:
"This is a very long string that..."
Run Code Online (Sandbox Code Playgroud)
我从这开始,但显然这不涉及在空格上结束字符串的问题.
<%= item.description[0..30] %>…
Run Code Online (Sandbox Code Playgroud) 我正在抓取一些网站,并有ASCII文本,我想转换为纯文本存储在数据库中.比如我想要的
I have got to tell anyone who will listen that this is
one of THE best adventure movies I've ever seen.
It's almost impossible to convey how pumped I am
now that I've seen it.
Run Code Online (Sandbox Code Playgroud)
转换成
I have got to tell anyone who will listen that this is
one of THE best adventure movies I've ever seen. It's
almost impossible to convey how pumped I am now that
I've seen it.
Run Code Online (Sandbox Code Playgroud)
我用手指搜索血腥,有什么帮助吗?