2015-12-09 136 views
2

與新的格局正則表達式我得到了我沒有能夠解決的問題的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,13替換爲值fruits[0]fruits[1]fruits[3]然後再轉換陣列成一個完整的串

但我認爲這不是最好的解決辦法,因爲如果我得到了一個輸入句子是這樣的:"2[2]"那麼我會收到輸出AppleApple,而展望是2Apple

+3

如果您的問題「無法解決」你爲什麼要張貼? – Moob

回答

1

如果你能夠改變paragraph的格式,那麼你可以從這段代碼開始。

String paragraph = "My most favorite fruit is %s, but I also like %s and %s"; 
String[] fruits = {"Banana", "Orange", "Apple", "Grape"}; 
System.out.printf(paragraph, fruits[0], fruits[1], fruits[2]);     

輸出

My most favorite fruit is Banana, but I also like Orange and Apple 

編輯另一種解決方案,不必保持水果的位置參數可能。

String paragraph = "My most favorite fruit is {0}, but I also like {1} and {3}"; 
Object[] fruits = {"Banana", "Orange", "Apple", "Grape"}; 
MessageFormat mf = new MessageFormat(paragraph); 
System.out.println(mf.format(fruits)); 

輸出

My most favorite fruit is Banana, but I also like Orange and Grape 
+0

我想用source段落中的精確索引替換,就像[0]應該用水果[0]替換,等等...... – mrzenky

+0

@mrzenky這是否意味着佔位符可以是隨機順序的。例如。 '李四喜歡[2]和[1],但不喜歡[3]'應導致爲'李四喜歡蘋果和橘子,但不喜歡Grape'? – SubOptimal

+0

@mrzenky您可以使用該'%1 $ s'。參見[格式化語法(https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax)。 –

0

您可以使用下面的代碼(見demo):

ArrayList<String> fruits_arr = new ArrayList<String>(); // Just initializing the array 
     fruits_arr.add("Banana");   
     fruits_arr.add("Orange"); 
     fruits_arr.add("Apple"); 
     fruits_arr.add("Grape"); 
    String[] fruits = fruits_arr.toArray(new String[0]); 
    String s = "My most favorite fruit is [0], but I also like [1] and [3]"; 
    StringBuffer result = new StringBuffer(); 
    Matcher m = Pattern.compile("\\[(\\d+)\\]").matcher(s); // Initializing Matcher 
    while (m.find()) {       // Iterate over matches 
     int num = Integer.parseInt(m.group(1)); // If there is a match, Group 1 has digits 
     String replacement = ""; 
     if (num < fruits.length) {   // If the number is lower than fruit element count 
       replacement =fruits[num]; // Get the array element 
     } else { 
      replacement = m.group();  // Else, use the whole match (e.g. [9]) 
     } 
     m.appendReplacement(result, replacement); // Append this replacement 
    } 
    m.appendTail(result); 
    System.out.println(result.toString()); 

隨着\[(\d+)]你搭配[ + digits + ]任何字符串,並捕獲數字序列到組1.

+0

像你這樣使用PHP?我可以用於Java嗎? – mrzenky

+0

我已將代碼重新寫入Java。是的,它類似於PHP,JS和其他語言,我們在Replace函數的回調函數中執行字符串操作。 –

2

使用字符串。更換

String sentence = "My most favorite fruit is [0], but I also like [1] and [3]"; 
String[] replacements = {"Banana", "Orange", "Apple", "Grape"}; 
for(int i = 0; i < replacements.length; i++) 
    sentence = sentence.replace("[" + i + "]", replacements[i]); 
+0

非常感謝,它的工作原理 – mrzenky

0

您可以使用以下方法:

String str = "My most favorite fruit is [0], but I also like [1] and [3]"; 
    String[] fruits = { "Banana", "Orange", "Apple", "Grape" }; 

    for (int i = 0; i < fruits.length; i++) 
    { 
     str = str.replaceAll("\\[" + i + "\\]", fruits[i]); 
    } 
3

Java的string formatting有一個內置的語法這一點。一般格式爲:

%[argument_index$][flags][width][.precision]conversion 

因此您可以使用例如%1$s意味着第一個格式參數,%2s第二等的注意,索引都是一個,而不是從零開始。

例如

String[] fruits = {"Banana", "Orange", "Apple", "Grape"}; 
System.out.format(
    "My most favorite fruit is %1$s, but I also like %2$s and %4$s", 
    fruits);