- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove all non-alphabetical characters of a String in Java?
The split() method of the String class accepts a String value representing the delimiter and splits into array of tokens (words), treating the string between the occurrence of two delimiters as one token.
For example if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.
If the String does not contain the specified delimiter this method returns an array containing the whole string as element.
The regular expression “\W+” matches all the not alphabetical characters (punctuation marks, spaces, underscores and special symbols) in a string.
Therefore, to remove all non-alphabetical characters from a String −
Get the string.
Split the obtained string int to an array of String using the split() method of the String class by passing the above specified regular expression as a parameter to it.
This splits the string at every non-alphabetical character and returns all the tokens as a string array.
Join all the elements in the obtained array as a single string.
Example
import java.util.Scanner; public class RemovingAlphabet { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name: "); String str = sc.nextLine(); String[] stringArray = str.split("\W+"); String result = new String(); for(int i = 0; i < stringArray.length;i++){ result = result+ stringArray[i]; } System.out.println("Result: "+result); } }
Output
Enter your name: Krishna ^% Kasyap*@# Result: KrishnaKasyap
- Related Articles
- How to remove all non-alphanumeric characters from a string in MySQL?
- JavaScript Remove non-duplicate characters from string
- Removing all non-alphabetic characters from a string in JavaScript
- PHP program to remove non-alphanumeric characters from string
- Remove all characters of first string from second JavaScript
- Sum of the alphabetical values of the characters of a string in C++
- Removing n characters from a string in alphabetical order in JavaScript
- C++ Program to Remove all Characters in a String Except Alphabets
- Check if the characters of a given string are in alphabetical order in Python
- Remove newline, space and tab characters from a string in Java
- MySQL Query to remove all characters after last comma in string?
- Java program to find all duplicate characters in a string
- Number of non-unique characters in a string in JavaScript
- Write a Regular Expression to remove all special characters from a JavaScript String?
- Java code to print common characters of two Strings in alphabetical order
