java.lang.String.replace 问题的提示? [英] Hints for java.lang.String.replace problem?

查看:23
本文介绍了java.lang.String.replace 问题的提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换."通过,"在我想写入文件的字符串/双精度值中.

I would like to replace "." by "," in a String/double that I want to write to a file.

使用以下Java代码

double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176
System.out.println(myDouble);

String myDoubleString = "" + myDouble;
System.out.println(myDoubleString);

myDoubleString.replace(".", ",");
System.out.println(myDoubleString);

myDoubleString.replace('.', ',');
System.out.println(myDoubleString);

我得到以下输出

38.1882352941176
38.1882352941176
38.1882352941176
38.1882352941176

为什么replace不做它应该做的事情?我希望最后两行包含一个,".

Why isn't replace doing what it is supposed to do? I expect the last two lines to contain a ",".

我必须做/使用其他东西吗?建议?

Do I have to do/use something else? Suggestions?

推荐答案

您需要将新值赋回给变量.

You need to assign the new value back to the variable.

double myDouble = myObject.getDoubleMethod(); // returns 38.1882352941176
System.out.println(myDouble);

String myDoubleString = "" + myDouble;
System.out.println(myDoubleString);

myDoubleString = myDoubleString.replace(".", ",");
System.out.println(myDoubleString);

myDoubleString = myDoubleString.replace('.', ',');
System.out.println(myDoubleString);

这篇关于java.lang.String.replace 问题的提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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