0 0
WP Ad Inserter plugin
Read Time:4 Minute, 49 Second

In this post, we have discussed how to remove particular character from string in Java. Learn 3 simple step-by-step guides here.

Table of Contents

Logic behind me:

When we try to remove any particular character from a String, we need to find that particular character in that String, and then by using any approach or method we can remove that particular character.

Let’s see what are the approaches that we can use to remove the character from String.

How to remove a particular character from String in Java?

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

Also Read: Java Program To Remove Duplicate Words In A String – 3 Ways

Approach-1: Remove a particular character from String using replace( ) method

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.

/** * How To Remove Particular Character From String In Java using replace( ) method? */ package cf.java.string; import java.util.Scanner; public class RemoveCharFromAString { public static void main(String[] args) { String str, newStr; int pos; Scanner sc = new Scanner(System.in); System.out.print("Enter any String: "); str = sc.nextLine(); System.out.print("Enter any position of char of String you want to remove: "); pos = sc.nextInt(); newStr = str.substring(0, pos-1) + str.substring(pos, str.length()); System.out.println("String after remove particular character: " + newStr); } }
Code language: PHP (php)

Output:

Enter any String: Codingface Enter any position of char of String you want to remove: 5 String after remove particular character: Codigface
Code language: JavaScript (javascript)

Explanation:

Here we have accepted the position of the 5th character from the user by using the Scanner class and stored it in the pos variable.

As we know that index of any String starts from ‘0’. So to get the 5th no character, we have used pos-1 to get the 4th index of the given string which is actually the 5th character.

Finally, by using replace( ) method, we have replaced that particular character with an empty string which means indirectly we have removed that character.

Also Read: How To Check String Contains Special Characters In Java

Approach-2: Remove a particular character from String using substring( ) method

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 that will remove the particular character.

Let’s see the program now.

/** * How To Remove Particular Character From String In Java using substring? */ package cf.java.string; import java.util.Scanner; public class RemoveCharFromAString { public static void main(String[] args) { String str, newStr; int pos; Scanner sc = new Scanner(System.in); System.out.print("Enter any String: "); str = sc.nextLine(); System.out.print("Enter any position of char of String you want to remove: "); pos = sc.nextInt(); newStr = str.substring(0, pos-1) + str.substring(pos, str.length()); System.out.println("String after remove particular character: " + newStr); } }
Code language: PHP (php)

Output:

Enter any String: Codingface Enter any position of char of String you want to remove: 5 String after remove particular character: Codigface
Code language: JavaScript (javascript)

Explanation:

Here we have converted the given String into two substrings. The first substring will contain characters from the first character to just the previous character of the given character which we have to remove and the second substring will contain characters from the next character of the given character to the last character of the given String.

Here we have taken, newStr = str.substring(0, pos-1) + str.substring(pos, str.length());

Here, we have taken the first substring from index ‘0’ to ‘3’. As per the example which is ‘Codi‘ and then the second substring from index ‘5’ to ‘9’ which is ‘gface‘.

Finally, we will append two substrings into a new string and display a new string after removal to the user.

Also Read: Java Program To Print Vowels In A String – 2 Simple Programs

People also ask for:

  1. How to remove a character from a string in Java?
  2. How to remove character from string in Java?
  3. How to remove a particular character from a string in Java?
  4. How to write a method which will remove any given character from a string in java?

Source: https://codingface.com/how-to-remove-particular-character-from-string-in-java/

How To Run Java Program In EditPlus – Know 2 Easy Steps Previous post How To Run Java Program In EditPlus – Know 2 Easy Steps
How to convert Array to XML in Java – 2 easy way Next post How to convert Array to XML in Java – 2 easy way

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.