用android中的字符串中的另一个字符替换字符? [英] Replacing a character by another character in a string in android?

查看:132
本文介绍了用android中的字符串中的另一个字符替换字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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..

请帮助!!

推荐答案

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

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...)

这篇关于用android中的字符串中的另一个字符替换字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆