如何与数组中的每个字符串进行比较

时间:2018-02-15 06:14:55

标签: ruby

我有一个由句子组成的数组:

arr = ["Independence day - 04 July",
       "Valentine's day - 14 February",
       "Christmas - 25 December"]

如果arr包含"04 July",我想打印"on 04 July is Independence day"

6 个答案:

答案 0 :(得分:2)

arr.map { |str| str.gsub(/(.+) - (.+)/, '\2 is \1') }
  #=> ["04 July is Independence day",
  #    "14 February is Valentine's day",
  #    "25 December is Christmas"]

正则表达式读取,"匹配捕获组1中的一个或多个字符,后跟字符串' - ',后跟捕获组2中的一个或多个字符"。 '\2 is \1'形成由捕获组2的内容组成的字符串,后跟字符串' is ',后跟捕获组1的内容。

如果连字符周围没有空格或多余的空格,请将正则表达式更改为

/(.+) *- *(.+)/

答案 1 :(得分:1)

据我了解,你需要这样的东西:

if value is '04 July' #=> On 04 July is Independence Day
if value is '14 February' #=> On 14 February is Valentin's day
if value is '25 December' #=> On 25 December is Christmas

试试这个

arr = ["Independence day - 04 July",
   "Valentin's day - 14 February",
   "Christmas - 25 December"]

value = '04 July'
arr.each do |element|
  festival, date = element.split('-').map(&:strip)
  if date.eql?(value)
    puts "On #{date} is #{festival}"
  end
end

我希望这会有所帮助!

答案 2 :(得分:0)

<?php //FILE: sms_api.php 
    function sendSMS($number, $message) { 
            $url = "example"; // Set your frontlinesms or frontlinecloud webconnection url here
            $secret = "secret"; // Set the secret here
            $request = array( 
                            'secret' => $secret, 
                            'message' => $message, 
                            'recipients' => array(array( 
                                    'type' => 'mobile', 
                                    'value' => $number 
                            ))
                    );  
            $req = json_encode($request);
            $ch = curl_init( $url );  
            curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
            curl_setopt( $ch, CURLOPT_POST, true );  
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $req );  
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
            $result = curl_exec($ch); 
            curl_close($ch);
            return split(',',$result); 
    }
    ?>

    <?php //FILE: index.php 
    include 'sms_api.php'; 
    $number = '+123456789'; 
    $text = 'Hi There, how are you?'; 
    $sms_api_result = sendSMS($number, $text);
    // Check if SMS was sent. The $sms_api_result boolean indicates whether the API call was successful.
    // You can replace the code below with custom handling logic
    if ($sms_api_result[0] == 'OK') { 
    // Ok, SMS received by the API 
            echo 'The SMS was sent.'; 
    } 
    else { 
    // Failure, SMS was not sent 
    // In this example we display the response to identify the error 
            print_r($sms_api_result); 
    } 
    ?>

答案 3 :(得分:0)

试试这个

puts 'on 04 July is Independence day' if arr.join(',').include?('04 July')

或者

string = '04 July'
arr.map{ |item| puts "on #{string} is a #{item.sub(" - #{string}", '')}" if item.include?string }

或以其他方式

string = '04 July'
items = arr.select{|item| item.include?(string)} 
puts "on #{string} is a #{items.first.sub(" - #{string}", '')}" unless items.empty?

答案 4 :(得分:0)

这是Enumerable#grep存在的原因之一

arr = ["Independence day - 04 July",
       "Valentin's day - 14 February",
       "Christmas - 25 December"]

str = "04 July"

arr.grep(Regexp.new(str)) # => ["Independence day - 04 July"]

这会找到匹配的行。然后你只需重新安排他们的部分进行演示。虽然如果你必须这样做,为什么不从一开始就单独存储零件?例如,在两个元素或哈希的数组中。

答案 5 :(得分:-1)

试试这个

compare_value = '04 July'
arr.each do |str_val|
  puts str_val if str_val.include?(compare_value)
end