Delete the last two characters of the String

TheBook picture TheBook · Jun 8, 2015 · Viewed 130.8k times · Source

How can I delete the last two characters 05 of the simple string?

Simple:

"apple car 05"

Code

String[] lineSplitted = line.split(":");
String stopName = lineSplitted[0];
String stop =   stopName.substring(0, stopName.length() - 1);
String stopEnd = stopName.substring(0, stop.length() - 1);

orginal line before splitting ":"

apple car 04:48 05:18 05:46 06:16 06:46 07:16 07:46 16:46 17:16 17:46 18:16 18:46 19:16

Answer

Ankur Singhal picture Ankur Singhal · Jun 8, 2015

Subtract -2 or -3 basis of removing last space also.

 public static void main(String[] args) {
        String s = "apple car 05";
        System.out.println(s.substring(0, s.length() - 2));
    }

Output

apple car