Remove extra delimiter from a String in Java

11 Dec, 2018
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  public class GFG {      public static void main(String args[])    {          // Get the String        String str = "Geeks, For, Geeks,";          // Get the delimiter        char delimiter = ',';          // Print the original string        System.out.println("Original String: "                           + str);          // Get the index of  delimiter        int index = 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
My Personal Notes
arrow_drop_upSave
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  public class GFG {      public static void main(String args[])    {          // Get the String        String str = "Geeks, For, Geeks,";          // Get the delimiter        char delimiter = ',';          // Print the original string        System.out.println("Original String: "                           + str);          // Get the index of  delimiter        int index = 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
My Personal Notes
arrow_drop_upSave
Below is the implementation of the above approach:
// Java program to remove// extra delimiter at the end of a String  public class GFG {      public static void main(String args[])    {          // Get the String        String str = "Geeks, For, Geeks,";          // Get the delimiter        char delimiter = ',';          // Print the original string        System.out.println("Original String: "                           + str);          // Get the index of  delimiter        int index = 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);    }}
Original String: Geeks, For, Geeks,
String with extra delimiter removed: Geeks, For, Geeks

Java-lang package
Java-String-Programs
Java-Strings
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java