Replacing a character by another character in a string in android?

后端 未结 3 809
有刺的猬
有刺的猬 2021-02-07 08:27

Simply i want to replace a character with another in android.. My code:

et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
str.         


        
相关标签:
3条回答
  • 2021-02-07 08:32

    Strings are immutable in Java - replace doesn't change the existing string, it returns a new one. You want:

    str = str.replace(' ','_');
    

    (This is definitely a duplicate, but I don't have enough time right now to find an appropriate one...)

    0 讨论(0)
  • 2021-02-07 08:50

    See code:

    et = (EditText) findViewById(R.id.editText1);
    String str = et.getText().toString();
    str = str.replace(' ', '_');
    System.out.println(str);
    
    0 讨论(0)
  • 2021-02-07 08:51

    String is immutable and you cannot change it. So, you need to do this:

    str = str.replace(' ','_');
    
    0 讨论(0)
提交回复
热议问题