2013-05-05 84 views
3

我有以下字符串:在相當長的字符串替換字符串:

你在哪裏[員工姓名]? 您有[SHIFT]換檔......

,幷包含字符串列表:

員工姓名

2.

我需要在長字符串中查找列表中給定的字符串,並將其替換爲另一個內容(包括[]字符)。 因此,例如第一個字符串需要更改爲:

你在哪裏綠色? 你有一個早班...

有沒有簡單的方法來做到這一點?使用IndexOf將給我這個字符串的位置,但我將如何包括[]人物?

UPDATE: 這是我測試至今代碼:

Scanner sc = new Scanner(smsText); 

    for (String s; (s = sc.findWithinHorizon("(?<=\\[).*?(?=\\])", 0)) != null;) 
    { 
     words.add(s); 
    } 

    for (int j = 0; j < words.size(); j++) 
    { 
     Log.d(TAG, "The value for column: "+words.get(j) +" is: "+ rowData.getValue(words.get(j))); 
     smsText.replaceFirst("\\[" + words.get(j) + "\\]", rowData.getValue(words.get(j))); 
    } 

    Log.d(TAG, "Final String is: "+ smsText); 

這是不給我正確的結果,該字符串不會被替換。

UPDATE2: 爲我工作的解決方案是:

Scanner sc = new Scanner(smsText); 

    for (String s; (s = sc.findWithinHorizon("(?<=\\[).*?(?=\\])", 0)) != null;) 
    { 
     columnNames.add(s); 
    } 

    for (int j = 0; j < columnNames.size(); j++) 
    { 
     Log.d(TAG, "The value for column: "+columnNames.get(j) +" is: "+ rowData.getValue(columnNames.get(j))); 
     smsText = smsText.replaceFirst("\\[" + columnNames.get(j) + "\\]", rowData.getValue(columnNames.get(j))); 
    } 
    Log.d(TAG, "Final String is: "+ smsText); 

感謝所有您的幫助。

+0

如果你有超過模板控制,他們改用'%s'和使用'的String.format()'。 – CommonsWare 2013-05-05 13:02:01

+0

@CommonsWare,不幸的是,我不知道,「[」和「]」裏面的短語是我需要從中提取替換內容的列名,所以它也不能被更改。 – 2013-05-05 13:04:39

+0

@EmilAdz是這個字符串'你在哪裏[員工姓名]?你有一個共同的[Shift] shift?你想用僱員的名字替換'[Employee Name]'嗎?讓我知道 – Pragnani 2013-05-05 13:08:51

回答

4
String key = myColumns.getName(); 
s.replaceFirst("\\[" + key + "\\]", myReplacements.getReplacement(key)); 

你也可以使用indexOf,而是用替換功能很不清楚你想要做什麼。

+0

Employee Name和Shift是代表列名稱的參數,並且會被更改(這只是一個例子),所以我需要一個更通用的解決方案。 – 2013-05-05 13:05:47

+0

@EmilAdz我更新了我的回答 – nullptr 2013-05-05 13:07:17

+0

謝謝@nullptr,給我幾分鐘來測試它。 – 2013-05-05 13:07:47

1

一個通用的解決方案可能是這個樣子:

String message = "Where are you [Employee Name]? You have a [Shift] shift!"; 
Map<String, String> variables = new HashMap<>(); 
variables.put("Employee Name", "John Green"); 
variables.put("Shift", "morning"); 
StringBuffer endResult = new StringBuffer(); 
Matcher m = Pattern.compile("\\[(.*?)\\]").matcher(message); 
while (m.find()) { 
    m.appendReplacement(endResult, variables.get(m.group(1))); 
} 
m.appendTail(endResult); 
System.out.println(endResult.toString()); 
2

試試這個

String s = "Where Are You [Employee Name]? your have a [Shift] shift.."; 
    Map<String, String> replacementMap = new HashMap<>(); 
    replacementMap.put("[Employee Name]", "John Green"); 
    replacementMap.put("[Shift]", "morning"); 
    for(Entry<String, String> e : replacementMap.entrySet()) { 
     s = s.replace(e.getKey(), e.getValue()); 
    } 
    System.out.println(s); 

輸出

Where Are You John Green? your have a morning shift.. 
+0

我需要替換這些字符串,而不是像上一個問題那樣找到它們。 – 2013-05-05 13:17:12

+0

@EmilAdz當您找到它們時很容易替換請檢查http://www.browxy.com/SubmittedCode/18488 – Pragnani 2013-05-05 13:19:43

1

我知道正則表達式是存在的,但如果你想要去的遞歸功能這裏是

public string replaceString(string str, string[] values, int index) 
    { 
     if (str.IndexOf('[') == -1 || str.IndexOf(']') == -1 || index > values.Length-1) 
      return str; 
     else 

      return replaceString(str.Replace(str.Substring(str.IndexOf('['), (str.IndexOf(']') - str.IndexOf('['))+1), values[index]), values, ++index); 
    } 

調用此方法

 string strforreplac = "Where Are You [Employee Name]? your have a [Shift] shift...]"; 
      string[] strvalues = {"Emil","morning"}; 
     string newstring = replaceString(strforreplac,strvalues,0);