Reputation: 561
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!!
Upvotes: 16
Views: 44052
Reputation: 314
See code:
et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
str = str.replace(' ', '_');
System.out.println(str);
Upvotes: 1
Reputation: 117589
String is immutable and you cannot change it. So, you need to do this:
str = str.replace(' ','_');
Upvotes: 6
Reputation: 1499900
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...)
Upvotes: 56