【问题标题】:How to search for and replace a whole phrase in a string array?如何在字符串数组中搜索和替换整个短语?
【发布时间】:2015-01-19 18:07:11
【问题描述】:

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

  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", "");

但是这句话仍然没有被删除。

【问题讨论】:

  • replaceAll 阅读相关文档。
  • 您的意见是什么? result[]的内容是什么?

标签: java android arrays


【解决方案1】:

您无需拆分字符串即可替换元素。 您可以执行以下操作:

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

如果您出于某种原因需要保留拆分,并且如果您在使用正则表达式时遇到问题,请查看here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    相关资源
    最近更新 更多