在this問題,提問者請求將插入空格字符的每x數量的解決方案。答案都涉及使用正則表達式。如果沒有正則表達式,你會怎麼做到這一點?插入這是每一個X字符數沒有正則表達式
這就是我想出了,但它是一個有點拗口。更簡潔的解決方案?
string = "12345678123456781234567812345678"
new_string = string.each_char.map.with_index {|c,i| if (i+1) % 8 == 0; "#{c} "; else c; end}.join.strip
=> "12345678 12345678 12345678 12345678"
爾加。打我幾秒鐘:) – thorncp 2010-07-02 21:45:22