将字符串截断为前n个单词

时间:2021-09-08 21:36:17

What's the best way to truncate a string to the first n words?

将字符串截断为前n个单词的最佳方法是什么?

4 个解决方案

#1


39  

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"]

#2


9  

You could do it like this:

你可以这样做:

s     = "what's the best way to truncate a ruby string to the first n words?"
n     = 6
trunc = s[/(\S+\s+){#{n}}/].strip

if you don't mind making a copy.

如果你不介意复制。

You could also apply Sawa's Improvement (wish I was still a mathematician, that would be a great name for a theorem) by adjusting the whitespace detection:

您也可以通过调整空格检测来应用Sawa的改进(希望我仍然是数学家,这将是一个定理的一个伟大名称):

trunc = s[/(\s*\S+){#{n}}/]

If you have to deal with an n that is greater than the number of words in s then you could use this variant:

如果你必须处理大于s中单词数的n,那么你可以使用这个变体:

s[/(\S+(\s+)?){,#{n}}/].strip

#3


4  

This could be following if it's from rails 4.2 (which has truncate_words)

如果它来自rails 4.2(具有truncate_words),这可能会跟随

string_given.squish.truncate_words(number_given, omission: "")

#4


3  

You can use str.split.first(n).join(' ') with n being any number.

你可以使用str.split.first(n).join(''),其中n是任意数字。

Contiguous white spaces in the original string are replaced with a single white space in the returned string.

原始字符串中的连续空格将替换为返回字符串中的单个空格。

For example, try this in irb:

例如,在irb中尝试:

>> a='apple    orange pear banana   pineaple  grapes'
=> "apple    orange pear banana   pineaple  grapes"
>> b=a.split.first(2).join(' ')
=> "apple orange"

This syntax is very clear (as it doesn't use regular expression, array slice by index). If you program in Ruby, you know that clarity is an important stylistic choice.

这种语法非常清楚(因为它不使用正则表达式,数组按索引切片)。如果您使用Ruby编程,您就会知道清晰度是一种重要的风格选择。

A shorthand for join is * So this syntax str.split.first(n) * ' ' is equivalent and shorter (more idiomatic, less clear for the uninitiated).

连接的简写是*所以这个语法str.split.first(n)*''是等价的和更短的(更惯用,对于不熟悉的人来说不太清楚)。

You can also use take instead of first so the following would do the same thing

您也可以使用take而不是first,以便以下内容执行相同的操作

a.split.take(2) * ' '

#1


39  

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"]

#2


9  

You could do it like this:

你可以这样做:

s     = "what's the best way to truncate a ruby string to the first n words?"
n     = 6
trunc = s[/(\S+\s+){#{n}}/].strip

if you don't mind making a copy.

如果你不介意复制。

You could also apply Sawa's Improvement (wish I was still a mathematician, that would be a great name for a theorem) by adjusting the whitespace detection:

您也可以通过调整空格检测来应用Sawa的改进(希望我仍然是数学家,这将是一个定理的一个伟大名称):

trunc = s[/(\s*\S+){#{n}}/]

If you have to deal with an n that is greater than the number of words in s then you could use this variant:

如果你必须处理大于s中单词数的n,那么你可以使用这个变体:

s[/(\S+(\s+)?){,#{n}}/].strip

#3


4  

This could be following if it's from rails 4.2 (which has truncate_words)

如果它来自rails 4.2(具有truncate_words),这可能会跟随

string_given.squish.truncate_words(number_given, omission: "")

#4


3  

You can use str.split.first(n).join(' ') with n being any number.

你可以使用str.split.first(n).join(''),其中n是任意数字。

Contiguous white spaces in the original string are replaced with a single white space in the returned string.

原始字符串中的连续空格将替换为返回字符串中的单个空格。

For example, try this in irb:

例如,在irb中尝试:

>> a='apple    orange pear banana   pineaple  grapes'
=> "apple    orange pear banana   pineaple  grapes"
>> b=a.split.first(2).join(' ')
=> "apple orange"

This syntax is very clear (as it doesn't use regular expression, array slice by index). If you program in Ruby, you know that clarity is an important stylistic choice.

这种语法非常清楚(因为它不使用正则表达式,数组按索引切片)。如果您使用Ruby编程,您就会知道清晰度是一种重要的风格选择。

A shorthand for join is * So this syntax str.split.first(n) * ' ' is equivalent and shorter (more idiomatic, less clear for the uninitiated).

连接的简写是*所以这个语法str.split.first(n)*''是等价的和更短的(更惯用,对于不熟悉的人来说不太清楚)。

You can also use take instead of first so the following would do the same thing

您也可以使用take而不是first,以便以下内容执行相同的操作

a.split.take(2) * ' '