• Register
0 votes
27 views
I am finding the answers in several blog for removing particular character from string in java.
4k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

I am going to show you the exact script for removing particular character from string in java. This script should solve your problem.

Code: In your removeChar .java file

public class RemoveChar {  
    public static void main(String[] args) {  
              String str = "Bangladesh is my country";  
              System.out.println(charRemoveAt(str, 7));  
           }  
           public static String charRemoveAt(String str, int p) {  
              return str.substring(0, p) + str.substring(p + 1);  
           }  
}  

The OutPut Should be :

Bangladesh is my country

 

31.1k points
0 votes

We can use the below 2 approaches to remove any particular character from a String in Java:

  1. How to remove particular character from String in java using replace( ) method
  2. How to remove particular character from String in Java using substring( ) method

Approach-1:

In this approach, we will find a particular character from the given String by using the charAt( ) method and then by using replace( ) method, we can remove a particular character from the given String.

newStr = str.replace(String.valueOf(str.charAt(pos-1)), "");

You can refer complete program here.

Approach-2:

In this approach, we will find 2 substrings of a given String in such a way that so it will not include the character which we have to remove and then we will append these two Strings to a new String which will remove the particular character.

newStr = str.substring(0, pos-1) + str.substring(pos, str.length());

You can refer complete program at the above link.

140 points