Write a program to input a string and a character, and remove that character from the given String. Print the final string.
Input and Output Format
- Input consists of a string and a character.
- The output consists of a string(the character cleaned string).
Refer sample output for formatting specifications
Sample Input :
elephant
e
Sample Output:
Lphant
Program to remove a character from string using replace()
Following are the steps to remove a character from a string:
- Input string from the user.
- Get the character that needs to remove from the user.
- Now, call the replace() method to replace the specified character with an empty string.
import java.util.Scanner; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); String name=sc.nextLine(); String ch=sc.next(); System.out.println(name.replace(ch,"")); } }
Output
Remove Character from string using String Buffer
Following are the steps to remove a character from a string:
- Input string from the user.
- Input character that user wants to remove from the string.
- Pass both of them to getValues() method.
- Inside the method, create an empty string buffer to hold the string after removal.
- Iterate over the string and check if the character that has to be removed is not equal to the characters in the string. If this is true then append the character into a string buffer.
- At last, we will be able to remove characters from the string and print that string.
package com.demo3; import java.util.Scanner; import java.util.StringTokenizer; public class StringModify { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char c = sc.next().charAt(0); getValues(str,c); } private static void getValues(String str, char c) { StringBuffer sb = new StringBuffer(); for(int i=0;i<str.length();i++){ if(c!=str.charAt(i)){ sb.append(str.charAt((i))); } } System.out.println(sb); } }
Output
Remove characters without using built-in functions
Following are the steps to remove a character from a string:
- Input string from the user and convert it into a character array.
- Input character that user wants to remove from the string.
- Pass both of them to getValues() method.
- Inside the method, create an empty string.
- Iterate over the string and check if the character that has to be removed is not equal to the remaining character in the string. If it is true then add the character to the empty string.
- At last, print them. (Here, we will get the string after removing a specified character).
package com.demo3; import java.util.Scanner; import java.util.StringTokenizer; public class StringModify { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char c = sc.next().charAt(0); char a[] = str.toCharArray(); getValues(a,c); } private static void getValues(char a[], char c) { String s = ""; for(int i=0;i<a.length;i++){ if(a[i]!=c){ s = s + a[i]; } } System.out.println(s); } }
Output
Thus, in this way, we learn how to remove characters from a given string.