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
2
  • 1
    you need to assign back the output of BusDetails.replaceAll("—", ""); to BusDetails Commented Feb 20, 2014 at 5: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. Commented Feb 20, 2014 at 5:49

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

0
BusDetails= BusDetails.replaceAll("—", "");

you forgot to re assign variable

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.