简体   繁体   English

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

[英]How to search for and replace a whole phrase in a string array?

I am trying to search a string for a phrase of words, and then replace the phrase with a blank space. 我正在尝试在字符串中搜索单词短语,然后将该短语替换为空格。 Here is my code: 这是我的代码:

  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)));
 }

I have also tried : 我也尝试过:

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

But the phrase is still not removed. 但是,该短语仍然没有删除。

You don't need to split your string in order to replace elements. 您无需拆分字符串即可替换元素。 You can do something like: 您可以执行以下操作:

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

If you need to keep the splitting for some reason and If you have troubles with regex, take a look here . 如果您出于某种原因需要保持拆分,并且正则表达式遇到问题,请在此处查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM