将字符串截断为前 n 个单词的最佳方法是什么?
最佳答案
n = 3
str = "your long long input string or whatever"
str.split[0...n].join(' ')
=> "your long long"
str.split[0...n] # note that there are three dots, which excludes n
=> ["your", "long", "long"]
关于ruby - 将字符串截断为前 n 个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6235762/