與新的格局正則表達式我得到了我沒有能夠解決的問題的Java替換字符串與陣列
我有一個包含一些關鍵字需要與存儲在一個新的值來代替一個段落陣列
實施例:
段落: 「我的最喜歡的水果是[0],但我也喜歡[1]和[3]」
陣:水果= 「香蕉」, 「橙色」, 「蘋果」, 「葡萄」]
我的期望是:My most favorite fruit is Banana, but I also like Orange and Grape
你能幫我找到一個解決方案?
我已經試過我的句子轉換成字符串數組是這樣的:
["My most favorite fruit is ","[0]",", but I also like ","[1]"," and ","[3]"]
在那之後,我更換[0]
到0
,我得到這個:
["My most favorite fruit is ","0",", but I also like ","1"," and ","3"]
我傾向於將上述陣列中的0
,1
和3
替換爲值fruits[0]
,fruits[1]
,fruits[3]
然後再轉換陣列成一個完整的串
但我認爲這不是最好的解決辦法,因爲如果我得到了一個輸入句子是這樣的:"2[2]"
那麼我會收到輸出AppleApple,而展望是2Apple
如果您的問題「無法解決」你爲什麼要張貼? – Moob