如何从字符串数组中的Ruby 2.0获得价值 [英] How get value from array of strings in Ruby 2.0

查看:118
本文介绍了如何从字符串数组中的Ruby 2.0获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串数组:

 数组= [耐克,耐克蒸,耐克多愁善感,耐克的实力,
          顺利潜伏,阿迪达斯空气,阿迪达斯jogar,阿迪达斯多愁善感,阿迪达斯高跟鞋]

我想从这个字符串中提取像查询SQL。

例如,如果用户输入单词耐克。然后4个字符串应该是回报结果

 耐克,耐克蒸,耐克多愁善感,耐克的实力

例如,如果用户输入单词阿迪达斯。然后4个字符串应该是回报结果

 阿迪达斯空气,阿迪达斯jogar,阿迪达斯多愁善感,阿迪达斯高跟鞋

这可能吗?


解决方案

  array.grep(查询)

返回与查询匹配的数组子集。

I have this array of strings:

array = [ "nike air", "nike steam","nike softy" ,"nike strength",
          "smooth sleeper","adidas air","addidas jogar","adidas softy","adidas heels"]

I want to extract strings from this as SQL like query.

E.g if user enter word "nike". Then 4 strings should be return as result

           "nike air", "nike steam","nike softy" ,"nike strength"

E.g if user enter word "adidas". Then 4 strings should be return as result

           "adidas air","addidas jogar","adidas softy","adidas heels"

Is it possible?

解决方案

array.grep(query)

Returns the array subset that matches the query.

这篇关于如何从字符串数组中的Ruby 2.0获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆