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. You need to do this:
BusDetails = BusDetails.replaceAll("—", "");
Also: "standard practice" is to name variables with a lowercase first letter 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.