2016-08-30 47 views
0

我有一個這樣的數組:Rspec的陣列匹配部分字符串時間戳

["xxxxxxxxx", 448, "2016-08-29", 1, "Trading", 53, "Trading", 2016-06-03 00:00:00 -0500, 2016-06-03 00:00:00 -0500, "RBS", "USD", "United States Dollar", nil, 2016-10-08 00:00:00 -0500, 2016-09-08 00:00:00 -0500, "2016-09-08 00:00:00 -0500","lcz076", nil, 5, 2016-10-08 00:00:00 -0500, 2016-09-08 00:00:00 -0500, "9", 448, "P", nil] 

,我需要爲這個值僅"2016-09-08 00:00:00"

我怎麼做匹配值?

+0

你只是要檢查它是否是裏面?然後使用'.include?' –

回答

0

首先將所有的日期,Strings

然後之後將其設置爲可變a

a = ["xxxxxxxxx", 448, "2016-08-29", 1, "Trading", 53, "Trading", "2016-06-03 00:00:00 -0500, 2016-06-03 00:00:00 -0500"...

我注意到所有類型的日期你想檢查有-0500這樣你就可以通過在此陣列中循環取出該部分

new_str[i] = str.slice(0..(str.index('-0500')))

現在你可以使用.include?("stuff here")

+0

它總是最後5位數,所以如何切片最後5位而不是 –

+0

請參閱http://stackoverflow.com/questions/4486709/how-do-i-remove-substring-after-a -ertain-character-in-a-string-using-ruby,你會學到很多東西。 –

0

假設你有:

a = ["xxxxxxxxx", 448, "2016-08-29", 1, "Trading", 53, "Trading", "2016-06-03 00:00:00 -0500", "2016-06-03 00:00:00 -0500", "RBS", "USD", "United States Dollar", nil, "2016-10-08 00:00:00 -0500", "2016-09-08 00:00:00 -0500", "2016-09-08 00:00:00 -0500", "lcz076", nil, 5, "2016-10-08 00:00:00 -0500", "2016-09-08 00:00:00 -0500", "9", 448, "P", nil] 

RSpec的預期應該是:

matches = a.select{ |x| x.to_s.match("2016-09-08 00:00:00") } 
expect(matches).not_to be_empty