Ruby on rails 确定忽略逗号/句点的字符串数组长度(仅限字母)
我试图找到确定字符串数组中字母数的最佳方法。我正在拆分字符串,然后循环每个单词,然后拆分字母并循环这些字母 当我确定长度时,我遇到的问题是它也在计算逗号和句点。因此,仅以字母表示的长度是不准确的 我知道使用regex可能会短得多,但我还不太精通这一点。我的代码通过了大多数测试,但我在计算逗号的地方卡住了Ruby on rails 确定忽略逗号/句点的字符串数组长度(仅限字母),ruby-on-rails,arrays,ruby,string,Ruby On Rails,Arrays,Ruby,String,我试图找到确定字符串数组中字母数的最佳方法。我正在拆分字符串,然后循环每个单词,然后拆分字母并循环这些字母 当我确定长度时,我遇到的问题是它也在计算逗号和句点。因此,仅以字母表示的长度是不准确的 我知道使用regex可能会短得多,但我还不太精通这一点。我的代码通过了大多数测试,但我在计算逗号的地方卡住了 E.g. 'You,' should be string.length = "3" 示例代码: def abbr(str) new_words = [] str.split.each
E.g. 'You,' should be string.length = "3"
示例代码:
def abbr(str)
new_words = []
str.split.each do |word|
new_word = []
word.split("-").each do |w| # it has to be able to handle hyphenated words as well
letters = w.split('')
if letters.length >= 4
first_letter = letters.shift
last_letter = letters.pop
new_word << "#{first_letter}#{letters.count}#{last_letter}"
else
new_word << w
end
end
new_words << new_word.join('-')
end
new_words.join(' ')
def缩写(str)
生词=[]
str.split.each do|单词|
新单词=[]
word.split(“-”)。每个do | w |#都必须能够处理连字符的单词
字母=w.分割(“”)
如果字母长度>=4
第一个字母=字母.shift
最后一个字母=字母.pop
新单词试试这个:
def abbr(str)
str.gsub /\b\w+\b/ do |word|
if word.length >= 4
"#{word[0]}#{word.length - 2}#{word[-1]}"
else
word
end
end
end
gsub
调用中的正则表达式表示“一个或多个单词字符前面和后面有单词边界”。传递给gsub的块对每个字都进行操作,从块返回的内容将替换gsub中的“字”匹配项。尝试以下操作:
def abbr(str)
str.gsub /\b\w+\b/ do |word|
if word.length >= 4
"#{word[0]}#{word.length - 2}#{word[-1]}"
else
word
end
end
end
arr = ["Now is the time for y'all Rubiests",
"to come to the aid of your bowling team."]
arr.join.size
#=> 74
gsub
调用中的正则表达式表示“一个或多个单词字符前面和后面有单词边界”。传递给gsub的块对每个字进行操作,来自块的返回是gsub中“字”匹配的替换
arr = ["Now is the time for y'all Rubiests",
"to come to the aid of your bowling team."]
arr.join.size
#=> 74
没有正则表达式
def abbr(arr)
str = arr.join
str.size - str.delete([*('a'..'z')].join + [*('A'..'Z')].join).size
end
abbr arr
#=> 58
R = /
[^a-z] # match a character that is not a lower-case letter
/ix # case-insenstive (i) and free-spacing regex definition (x) modes
def abbr(arr)
arr.join.gsub(R,'').size
end
abbr arr
#=> 58
在此处和下方,arr.join
将数组转换为单个字符串
带有正则表达式
def abbr(arr)
str = arr.join
str.size - str.delete([*('a'..'z')].join + [*('A'..'Z')].join).size
end
abbr arr
#=> 58
R = /
[^a-z] # match a character that is not a lower-case letter
/ix # case-insenstive (i) and free-spacing regex definition (x) modes
def abbr(arr)
arr.join.gsub(R,'').size
end
abbr arr
#=> 58
你当然可以写:
arr.join.gsub(/[^a-z]/i,'').size
#=> 58
没有正则表达式
def abbr(arr)
str = arr.join
str.size - str.delete([*('a'..'z')].join + [*('A'..'Z')].join).size
end
abbr arr
#=> 58
R = /
[^a-z] # match a character that is not a lower-case letter
/ix # case-insenstive (i) and free-spacing regex definition (x) modes
def abbr(arr)
arr.join.gsub(R,'').size
end
abbr arr
#=> 58
在此处和下方,arr.join
将数组转换为单个字符串
带有正则表达式
def abbr(arr)
str = arr.join
str.size - str.delete([*('a'..'z')].join + [*('A'..'Z')].join).size
end
abbr arr
#=> 58
R = /
[^a-z] # match a character that is not a lower-case letter
/ix # case-insenstive (i) and free-spacing regex definition (x) modes
def abbr(arr)
arr.join.gsub(R,'').size
end
abbr arr
#=> 58
你当然可以写:
arr.join.gsub(/[^a-z]/i,'').size
#=> 58
您可以检查每个字符的ascii值是97-122还是65-90。当满足此条件时,增加一个局部变量,该变量将为您提供字符串的总长度,而不包含任何数字、任何特殊字符或任何空格。您可以检查每个字符的ascii值是97-122还是65-9065-90.当满足此条件时,增加一个局部变量,该变量将为您提供字符串的总长度,而不包含任何数字、任何特殊字符或任何空格。您可以使用类似的方法(短版本):
带说明的长版本:
a = ['a, ', 'bbb', 'c c, .'] # Initial array of strings
excluded_chars = [' ', ',', '.'] # Chars you don't want to be counted
a.map do |str| # Iterate through all strings in array
str.chars.reject do |char| # Split each string to the array of chars
excluded_chars.include? char # and reject excluded_chars from it
end.size # This returns [["a"], ["b", "b", "b"], ["c", "c"]]
end # so, we take #size of each array to get size of the string
# Result: [1, 3, 2]
您可以使用类似的内容(简短版本):
带说明的长版本:
a = ['a, ', 'bbb', 'c c, .'] # Initial array of strings
excluded_chars = [' ', ',', '.'] # Chars you don't want to be counted
a.map do |str| # Iterate through all strings in array
str.chars.reject do |char| # Split each string to the array of chars
excluded_chars.include? char # and reject excluded_chars from it
end.size # This returns [["a"], ["b", "b", "b"], ["c", "c"]]
end # so, we take #size of each array to get size of the string
# Result: [1, 3, 2]
我很难确定abbr
方法的目的。你能证明这个方法的输入样本,然后你想返回什么吗?如果您只想快速计算字符串中的字母字符数:str.scan(/[:alpha:]]/).length
Sorry@LucasNelson-如果一个单词的长度为4或更多,则目标是将其缩写。例如,国际化=i18n
。所以我们得到第一个和最后一个字母,然后数一数中间的字母。我更新了代码,我不能扫描,因为它是一个数组(不是专家,所以我可能遗漏了什么)@LizGee,你能提供更多的例子吗?我也不太明白你需要做什么。我很难确定abbr
方法的目的。你能证明这个方法的输入样本,然后你想返回什么吗?如果您只想快速计算字符串中的字母字符数:str.scan(/[:alpha:]]/).length
Sorry@LucasNelson-如果一个单词的长度为4或更多,则目标是将其缩写。例如,国际化=i18n
。所以我们得到第一个和最后一个字母,然后数一数中间的字母。我更新了代码,我不能扫描,因为它是一个数组(不是专家,所以我可能遗漏了什么)@LizGee,你能提供更多的例子吗?我也不太明白你需要做什么。