【问题标题】:how to replace only one word in EditText如何只替换EditText中的一个单词
【发布时间】:2015-03-13 17:36:00
【问题描述】:

在我的应用程序中,我有一个 EditText,它的文本是“你好,我的朋友,你好”。我怎样才能用再见代替第二个你好?我想将其文本更改为“你好,我的朋友,再见”。我使用了 replace() 语句,但用再见替换了所有你好的话。我可以获得字母索引并用于替换吗?例如,我说要编程将 18 到 22 的字母替换为再见。 这是我的代码:

String text = edtText.getText().toString().replace("Hello", "goodbye");
edtText.setText(text);

【问题讨论】:

标签: java android replace android-edittext


【解决方案1】:

试试这个:

String actualString = "Hello my friend, Hello";
String finalString = actualString.substring(0, actualString.lastIndexOf("Hello")) +
                     "goodbye" +
                     actualString.substring(actualString.lastIndexOf("Hello") + "Hello".length(), actualString.length());

【讨论】:

    【解决方案2】:

    这应该可行:

    public static String replace(String phrase, String before, String after, int index) {
        String[] splittedPhrase = phrase.split(" ");
        String output = "";
        for (int i = 0, j = 0; i < splittedPhrase.length; i++) {
            if (i != 0)
                output += " ";
            if (splittedPhrase[i].equals(before) && index == j++)
                output += after;
            else
                output += splittedPhrase[i];
        }
        return output;
    }
    

    或者,用更短的方式:

    public static String replace(String phrase, String before, String after, int index) {
        int i = 0;
        String output = "";
        for (String s : phrase.split(" "))
            output += ((s.equals(before) && index == i++) ? after : s) + " ";
        return output.substring(0, output.length() - 1);
    }
    

    那么,简单地说:

    public static void main(String args[]) {
        System.out.println(replace("hello world, hello", "hello", "goodbye", 0));
        System.out.println(replace("hello world, hello", "hello", "goodbye", 1));
        System.out.println(replace("hello world, hello", "hello", "goodbye", 2));
    }
    

    打印出来:

    "goodbye world, hello"
    "hello world, goodbye"
    "hello world, hello"
    

    【讨论】:

      【解决方案3】:

      您的解决方案。

       et = (EditText) findViewById(R.id.editText);
       String text = "Hello my friend , Hello";
       et.setText(text);
       String[] txt = text.split("Hello");
       for (int x = 0 ; x < txt.length ; x++)
           System.out.println(txt[x]); //for cast
       text = "goodbye" + txt[0] + txt[1] + "goodbye";
       et.setText(text);
      

      围绕给定正则表达式的匹配拆分此字符串。

      此方法的工作方式就像通过使用给定表达式和零限制参数调用双参数拆分方法一样。因此,尾随的空字符串不包含在结果数组中。 Font

      【讨论】:

        【解决方案4】:

        试试这个:

        string hello = "hello";
        int start =0;
        int end =0;
        String text = edtText.getText().toString();
        String replacement = "replacement";
        start=text.indexOf(hello, str.indexOf(hello) + 1);
        end=start+hello.lenght();
        
        StringBuilder s = new StringBuilder();
         s.append(text.substring(0, start)));
         s.append(replacement);
         s.append(text.substring(end);
        edtText.setText(s.toString());
        

        【讨论】:

          猜你喜欢
          • 2012-05-06
          • 2019-12-30
          • 1970-01-01
          • 1970-01-01
          • 2015-03-02
          • 1970-01-01
          • 2016-01-03
          • 2018-10-30
          • 2014-05-18
          相关资源
          最近更新 更多