Given a String with extra delimiter at the end, the task is to remove this extra delimiter in Java.
Examples:
Input: String = "Geeks, For, Geeks, ", delimiter = ', ' Output: "Geeks, For, Geeks" Input: String = "G.e.e.k.s.", delimiter = '.' Output: "G.e.e.k.s"
Approach:
- 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ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String args[])   Â{       Â// Get the String       ÂString str ="Geeks, For, Geeks,";       Â// Get the delimiter       Âchardelimiter =',';       Â// Print the original string       ÂSystem.out.println("Original String: "                          Â+ str);       Â// Get the index of delimiter       Âintindex = str.lastIndexOf(delimiter);       Â// Remove the extra delimiter by skipping it       Âstr = str.substring(0, index)             Â+ str.substring(index +1);       Â// Print the new String       ÂSystem.out.println("String with extra "                          Â+"delimiter removed: "                          Â+ str);   Â}}Output:Original String: Geeks, For, Geeks, String with extra delimiter removed: Geeks, For, Geeks
