[英]Unable to replace in String java ?
I am unable to replace part of the substring in my code ? 我无法替换代码中的部分子字符串? I want to get rid of the unwanted characters but i still get the same output ?
我想摆脱不需要的字符,但我仍然得到相同的输出?
String BusDetails = "ROUTE 3 — CLEARBROOK-UFV GOLINE TO UFV" ;
System.out.println("BusDetails before"+BusDetails);
BusDetails.replaceAll("—", "");
System.out.println("BusDetails After"+BusDetails);
// Output
BusDetails before ROUTE 3 — CLEARBROOK-UFV GOLINE TO UFV
BusDetails After ROUTE 3 — CLEARBROOK-UFV GOLINE TO UFV
Java strings are immutable. Java字符串是不可变的。 You need to do this:
您需要这样做:
BusDetails = BusDetails.replaceAll("—", "");
Also: "standard practice" is to name variables with a lowercase first letter busDetails
. 另外:“标准做法”是用小写的首字母
busDetails
命名变量。
BusDetails= BusDetails.replaceAll("—", "");
你忘了重新分配变量
You need to put 'replaced value' into another variable. 您需要将“替换值”放入另一个变量。 Example below compiled code:
下面的例子编译代码:
String busDetails = "ROUTE 3 — CLEARBROOK-UFV GOLINE TO UFV" ;
System.out.println("BusDetails before :"+busDetails);
String replacetxt = busDetails.replaceAll("— ", "");
System.out.println("BusDetails After :"+replacetxt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.