我有一個字符串,可能在實際文本之前有一些\n
。我想在新行字符或字符後添加t->
。這意味着,一旦我想插入't->'的字符串中有'\ n'以外的其他字符。如何在' n'之後插入't->'?
有沒有什麼簡單的方法比
what = "\nrufus: ready" #result is "\nt->rufus: ready"
#what = "\n\nrufus: ready" #result is "\n\nt->rufus: ready"
#what = "\n\nrufus\n: ready" #result is "\n\nt->rufus: ready\n"
what.length.times do |i|
if (what[i,1] != "\n")
what.insert(i,"t->")
break
end
end
「\ n \ n」。長度總是2.您的代碼,做你想讓它做什麼?什麼是'什麼'? – steenslag 2012-01-04 01:55:05
你的問題不清楚。如果您將包含原始文本和想要獲得的內容,這會更有幫助。你想在所有行結束之前插入一些文本? – Phrogz 2012-01-04 01:56:56
「什麼」不是我聽說過的任何變數。他們在「What」中有換行符? – 2012-01-04 02:00:16