Saved Bookmarks
| 1. |
How can we remove a specific character from a String? |
|
Answer» There are several ways to remove a character from a string, such as REMOVING the character at the beginning of the string, the end of the string, or at a specific position. It is possible to remove a specific character from a string using the replace() method. You can also use remove() with different VARIATIONS like replaceFirst(), replaceAll(), etc. Below is a JAVA program that uses replace(), replaceFirst(), and replaceAll() methods to remove characters from a String. public CLASS RemoveCharacter{ public static void main(String args[]) { String str = "Scaler by InterviewBit"; //remove the specified character. System.out.println("String after removing 'e' = "+str.replace("e", "")); //remove the FIRST occurrence of the specified character. System.out.println("String after removing First 'e' = "+str.replaceFirst("e", "")); //remove all occurrences of the specified character. System.out.println("String after replacing all small letters = "+str.replaceAll("([A-Z])", "")); }}Output: String after removing 'e' = Scalr by IntrviwBitString after removing First 'e' = Scalr by InterviewBitString after replacing all small letters = caler by nterviewit |
|