如何搜索和替换字符串数组中的整个短语?

时间:2015-01-19 18:07:11

标签: java android arrays

我正在尝试在字符串中搜索单词的短语,然后用空格替换短语。这是我的代码:

  public void convertToNotes() {
    classNotes = (EditText) findViewById(R.id.class_notes);
    className = (EditText) findViewById(R.id.class_name);
    verifyTextView = (TextView) findViewById(R.id.textView);
    verifyTextView.setSingleLine(false);

    conversionText = classNotes.getText().toString();
    String[] result = conversionText.split(".");


    for (int a = 0; a < result.length; a++) {

        result[a] = result[a].replaceAll("forget"+ "about"+ "that", "");
        result[a] = result[a].replaceAll("important", " <br><u>IMPORTANT</u>");
        }
    verifyTextView.setText(Html.fromHtml(Arrays.toString(result)));
 }

我也尝试过:

result[a] = result[a].replaceAll("forget about that", "");

但这句话仍未删除。

1 个答案:

答案 0 :(得分:0)

您无需拆分字符串以替换元素。 你可以这样做:

conversionText = conversionText.replaceAll("forget" /*or other regex*/, "");
verifyTextView.setText(Html.fromHtml(conversionText));

如果由于某种原因需要保持分裂,如果你有正则表达式的麻烦,请看看here