2014-01-08 74 views
0

我試圖從字符串中看到類似「ABCDEFG [1]」的數字值。在這裏我試圖得到1.我試圖在下面的字符串上使用split方法,但dint獲得成功。任何解決方案,這是讚賞。謝謝!通過在jruby中分割進行字符串比較

+0

請發表你的努力給我們的地方,從開始的代碼。 – Shepmaster

+1

'ABCDEFG [1]「。match('\ d')'? – orde

+1

您是否嘗試過使用正則表達式? – sunnyrjuneja

回答

0

在這裏你去:

def get_numeric_value(str) 
    # Match a number surrounded by square brackets 
    matchdata = str.match(/\[\d+\]/) 
    if matchdata 
    # Get the first matched substring, and remove the first and last characters 
    # (which will be the square brackets) 
    number = matchdata[0][1..-2] 
    number.to_i 
    else 
    nil 
    end 
end 

get_numeric_value "ABCDEFG [1]" #=> 1 
+0

非常感謝您的幫助。我想將方括號中的數字與0進行比較,然後在大於或小於0時​​執行各種操作。我沒有使用正則表達式,而是使用「count」方法,它對我來說工作正常。 – itsh