Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete the last two characters of the String [duplicate]

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
like image 632
TheBook Avatar asked Jun 08 '15 11:06

TheBook


People also ask

How do I remove the last two characters of a string?

Use the String. substring() method to remove the last 2 characters from a string, e.g. const removedLast2 = str. substring(0, str. length - 2); .

How do I remove the last 3 characters from a string?

Use the String. substring() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. substring(0, str. length - 3); .

How can I remove last two characters from a string in C#?

C# String. Remove() method has two overloaded forms: Remove(Int32) - Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.


2 Answers

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
like image 152
Ankur Singhal Avatar answered Oct 16 '22 20:10

Ankur Singhal


Use String.substring(beginIndex, endIndex)

str.substring(0, str.length() - 2);

The substring begins at the specified beginIndex and extends to the character at index (endIndex - 1)

like image 28
Isuru Gunawardana Avatar answered Oct 16 '22 21:10

Isuru Gunawardana