remove specific character from string in java

Learn Java Programming for Beginners

import java.io.*;
import java.util.*;
import java.lang.*;
class remove_character
{
public static void main(String args[])throws IOException
{
String s,character;
char ch1,ch2;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter String: ");
s=in.readLine( );
System.out.println("");
System.out.print("Enter Specific character to remove: ");
character=in.readLine( );
System.out.println("");
for(int i=0;i<s.length();i++)
{
ch1=character.charAt(0);
ch2=s.charAt(i);
if(ch1==ch2)
{
s=removeCharAt(s,i);
}
}
System.out.println("String after removing specific character from string\t:"+s);
}
public static String removeCharAt(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 1);
}
}

remove specific characters from string in java OUTPUT:

Enter String: remove specific character from string

Enter Specific character to remove: a

String after removing specific character from string : remove specific chrcter from string

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *