Java String.replace无效 [英] Java String.replace not working

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

问题描述

这是我的代码。

String x = "1+√10";
x = x.replace(".*", "x");
System.out.println(x);

这应该返回x但istead它返回1 +& radic; 10。为什么这不起作用?

This should return "x" but istead it is returning "1+√10". Why isn't this working?

推荐答案

String#replace doesn' t支持正则表达式,使用字符串#replaceAll

String#replace doesn't support regex, use String#replaceAll:

x = x.replaceAll(".+", "x");

这篇关于Java String.replace无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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