Given a String with extra delimiter at the end, the task is to remove this extra delimiter in Java.
Input: String = "Geeks, For, Geeks, ", delimiter = ', ' Output: "Geeks, For, Geeks" Input: String = "G.e.e.k.s.", delimiter = '.' Output: "G.e.e.k.s"
- Get the String.
- Get the last index of the delimiter using lastIndexOf() method.
- Construct a new String with the 2 different substrings: one from beginning till the found index – 1, and the other from the index + 1 till the end.
Below is the implementation of the above approach:
// Java program to remove
// extra delimiter at the end of a String
// Get the String
String str =
"Geeks, For, Geeks,"
// Get the delimiter
// Print the original string
"Original String: "
// Get the index of delimiter
index = str.lastIndexOf(delimiter);
// Remove the extra delimiter by skipping it
str = str.substring(
+ str.substring(index +
// Print the new String
"String with extra "
"delimiter removed: "
Original String: Geeks, For, Geeks, String with extra delimiter removed: Geeks, For, Geeks
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.My Personal Notes arrow_drop_up
- Scanner delimiter() method in Java with Examples
- Trim (Remove leading and trailing spaces) a string in Java
- Remove Leading Zeros From String in Java
- Remove all non-alphabetical characters of a String in Java
- How to remove all non-alphanumeric characters from a string in Java
- How to remove all white spaces from a String in Java?
- Remove first and last character of a string in Java
- Different Ways to Remove all the Digits from String in Java
- Remove a given word from a String
- How to remove an element from ArrayList in Java?
- ArrayList and LinkedList remove() methods in Java with Examples
- TreeSet remove() Method in Java
- EnumMap remove() Method in Java
- LinkedList remove() Method in Java
- HashSet remove() Method in Java
- PriorityQueue remove() Method in Java
- HashMap remove() Method in Java
- TreeMap remove() Method in Java
- ArrayDeque remove() Method in Java
- WeakHashMap remove() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.