用android中的字符串中的另一个字符替换字符?

prg*_*prg 16 java android replace special-characters

我想在android中替换另一个角色..我的代码:

et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
str.replace(' ','_');
et.setText(str);
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)

但这里的"空间"并没有被"下划线"取代..我也尝试了其他角色..

请帮忙!!

Jon*_*eet 56

字符串在Java中是不可变的 - replace不会更改现有字符串,而是返回一个新字符串.你要:

str = str.replace(' ','_');
Run Code Online (Sandbox Code Playgroud)

(这绝对是重复的,但我现在没有足够的时间找到合适的......)


Eng*_*uad 6

字符串是不可变的,您无法更改它.所以,你需要这样做:

str = str.replace(' ','_');
Run Code Online (Sandbox Code Playgroud)


小智 1

参见代码:

et = (EditText) findViewById(R.id.editText1);
String str = et.getText().toString();
str = str.replace(' ', '_');
System.out.println(str);
Run Code Online (Sandbox Code Playgroud)