从字符串中获取前N个字符而不删除整个单词

时间:2023-01-06 21:35:35

I want to know if there an easy way to get only N symbols from string without cutting the whole words.

我想知道是否有一种简单的方法可以从字符串中仅获取N个符号而不会切割整个单词。

For example, I have products and products descriptions information. The description length is from 70 to 500 symbols, but I want to display only the first 70 symbols like this:

例如,我有产品和产品描述信息。描述长度是70到500个符号,但我想只显示前70个符号,如下所示:

Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world.

可口可乐是历史上最受欢迎和最畅销的软饮料,也是世界上最知名的品牌。

On May 8, 2011, Coca-Cola celebrated its 125thanniversary. Created in 1886 in Atlanta, Georgia, by Dr. John S. Pemberton, Coca-Cola was first offered as a fountain beverage at Jacob's Pharmacy by mixing Coca-Cola syrup with carbonated water.

2011年5月8日,可口可乐庆祝其125周年纪念日。佐治亚州约翰逊·彭伯顿博士于1886年在佐治亚州亚特兰大创立,可口可乐首先通过将可口可乐糖浆与碳酸水混合,在雅各布药房提供饮料饮料。

So, ordinary sub string method will give me:

那么,普通的子串方法会给我:

Coca-Cola is the most popular and biggest-selling soft drink in histor

and I need a method to get only this:

我需要一个方法来获得这个:

Coca-Cola is the most popular and biggest-selling soft drink in ...

6 个解决方案

#1


3  

s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
s = s.split(" ").each_with_object("") {|x,ob| break ob unless (ob.length + " ".length + x.length <= 70);ob << (" " + x)}.strip
#=> "Coca-Cola is the most popular and biggest-selling soft drink in"

#2


7  

Just use truncate with separator option:

只需使用truncate with separator选项:

truncate("Once upon a time in a world far far away", length: 17)
# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
# => "Once upon a..."

Get more info at: truncate helper in rails API documentation

在rails API文档中获取更多信息:truncate helper

#3


5  

This method uses a regexp which greedily grabs up to 70 characters and subsequently matchs a space or end of string to accomplish your goal

此方法使用正则表达式,贪婪地抓取多达70个字符,然后匹配字符串的空格或结尾以实现您的目标

def truncate(s, max=70, elided = ' ...')
  s.match( /(.{1,#{max}})(?:\s|\z)/ )[1].tap do |res|
    res << elided unless res.length == s.length
  end    
end

s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
truncate(s)
=> "Coca-Cola is the most popular and biggest-selling soft drink in ..."

#4


1  

s[0..65].rpartition(" ").first << " ..."

In your examle:

在你的考试中:

s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."    
t = s[0..65].rpartition(" ").first << " ..."
=> "Coca-Cola is the most popular and biggest-selling soft drink in ..." 

#5


0  

(Inspired by dbenhur's answer but better handles the case where there is no white space or end of string in the first max characters.)

(受dbenhur的回答启发,但更好地处理第一个最大字符中没有空格或字符串结尾的情况。)

def truncate(s, options = { })
  options.reverse_merge!({
     max: 70,
     elided: ' ...'
  });

  s =~ /\A(.{1,#{options[:max]}})(?:\s|\z)/
  if $1.nil? then s[0, options[:max]] + options[:elided]
  elsif $1.length != s.length then $1 + options[:elided]
  else $1
  end
end

#6


0  

b="Coca-Cola is the most popular and biggest-selling soft drink in history, as well "

def truncate x
a=x.split("").first(70).join

w=a.split("").map!.with_index do |x,y|
    if x!=" "
        x=nil
    else
        x=y
    end
end
w.compact!
index=w.last

x.split("").first(index).join+" ..."
end

truncate b

#1


3  

s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
s = s.split(" ").each_with_object("") {|x,ob| break ob unless (ob.length + " ".length + x.length <= 70);ob << (" " + x)}.strip
#=> "Coca-Cola is the most popular and biggest-selling soft drink in"

#2


7  

Just use truncate with separator option:

只需使用truncate with separator选项:

truncate("Once upon a time in a world far far away", length: 17)
# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
# => "Once upon a..."

Get more info at: truncate helper in rails API documentation

在rails API文档中获取更多信息:truncate helper

#3


5  

This method uses a regexp which greedily grabs up to 70 characters and subsequently matchs a space or end of string to accomplish your goal

此方法使用正则表达式,贪婪地抓取多达70个字符,然后匹配字符串的空格或结尾以实现您的目标

def truncate(s, max=70, elided = ' ...')
  s.match( /(.{1,#{max}})(?:\s|\z)/ )[1].tap do |res|
    res << elided unless res.length == s.length
  end    
end

s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."
truncate(s)
=> "Coca-Cola is the most popular and biggest-selling soft drink in ..."

#4


1  

s[0..65].rpartition(" ").first << " ..."

In your examle:

在你的考试中:

s = "Coca-Cola is the most popular and biggest-selling soft drink in history, as well as the best-known brand in the world."    
t = s[0..65].rpartition(" ").first << " ..."
=> "Coca-Cola is the most popular and biggest-selling soft drink in ..." 

#5


0  

(Inspired by dbenhur's answer but better handles the case where there is no white space or end of string in the first max characters.)

(受dbenhur的回答启发,但更好地处理第一个最大字符中没有空格或字符串结尾的情况。)

def truncate(s, options = { })
  options.reverse_merge!({
     max: 70,
     elided: ' ...'
  });

  s =~ /\A(.{1,#{options[:max]}})(?:\s|\z)/
  if $1.nil? then s[0, options[:max]] + options[:elided]
  elsif $1.length != s.length then $1 + options[:elided]
  else $1
  end
end

#6


0  

b="Coca-Cola is the most popular and biggest-selling soft drink in history, as well "

def truncate x
a=x.split("").first(70).join

w=a.split("").map!.with_index do |x,y|
    if x!=" "
        x=nil
    else
        x=y
    end
end
w.compact!
index=w.last

x.split("").first(index).join+" ..."
end

truncate b