0

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
dev_marshell08
  • 1,051
  • 3
  • 18
  • 40
  • 1
    you need to *assign back* the output of `BusDetails.replaceAll("—", "");` to `BusDetails` – Manish Feb 20 '14 at 05:47
  • 2
    Side note: Make sure you stick to java naming conventions. Variables should start with a lowercase, and class names should start with an uppercase and be CamelCase. So `BusDetails` should be named `busDetails`. – mdewitt Feb 20 '14 at 05:49

3 Answers3

5

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.

John3136
  • 28,809
  • 4
  • 51
  • 69
0
BusDetails= BusDetails.replaceAll("—", "");

you forgot to re assign variable

Yogesh
  • 4,546
  • 2
  • 32
  • 41
0

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);
Suzon
  • 749
  • 1
  • 8
  • 21