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.replace(' ','_');
et.setText(str);
System.out.println(str);
But here the "space" is not replaced by "underscore".. I also tried other character too..
please help!!
static String replaceString(String string) { return string. replaceAll("[^A-Za-z0-9 ]","");// removing all special character. } this is work great but if user will enter the other language instead of eng then this code will replace the char of other language.
String Replace() Method As the name itself suggests, the replace() method is used to replace all the occurrences of a specific character of a String with a new character.
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...)
String is immutable and you cannot change it. So, you need to do this:
str = str.replace(' ','_');
See code:
et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
str = str.replace(' ', '_');
System.out.println(str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With