Write a method which will remove any given character from a String?

Asked 27-Feb-2018
Viewed 356 times

1 Answer


1

We are using java language to solve your queries-

import java.util.*;
public class RemoveCharFromString
 {
    public static void main (String [] args)
        {
       Scanner sc=new Scanner(System.in);
             System.out.println("Enter Any String  : "); 
         String str=sc.nextLine();              System.out.println("Enter Any Character Which You Want to remove : ");         char ch = sc.next().charAt(0);           System.out.println (removeCharInString (str, ch));        } /* Here is a method which will remove any given character from a String   */
  public static String removeCharInString (String string, char charToBeRemoved) {         if (string == null)              return "";          StringBuilder strBuild = new StringBuilder ();         for (int i = 0; i < string.length (); i++) {             char chr = string.charAt (i);             if (chr == charToBeRemoved)                 continue;             strBuild.append (chr);         }         return strBuild.toString ();     } }