Use [^A-Za-z0-9].

Note: removed the space since that is not typically considered alphanumeric.

Answer from Mirek Pluta on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - GeeksforGeeks
July 15, 2025 - Since the alphanumeric characters lie in the ASCII value range of [65, 90] for uppercase alphabets, [97, 122] for lowercase alphabets, and [48, 57] for digits. Hence traverse the string character by character and fetch the ASCII value of each character. If the ASCII value is not in the above three ranges, then the character is a non-alphanumeric character.
Discussions

Trying to remove non-alphabetical characters from inputted string need help pls
The problem is that you're modifying the string while you're iterating over it. For example, if you have the input string 123, then on the first iteration i=0, so you'll remove the 1, resulting in the string 23. On the second iteration, i=1, and you'll remove the second character of the string which is 3. But you skipped the 2 because it was shifted backwards to an earlier index. You can hack around this, but it's simpler and more efficient to copy the input to a different variable and skip the non-alphabetical characters, instead of removing them in-place. More on reddit.com
🌐 r/learnprogramming
8
0
February 22, 2024
How to remove non-alphanumeric characters in Java?
Find answers to How to remove non-alphanumeric characters in Java? from the expert community at Experts Exchange More on experts-exchange.com
🌐 experts-exchange.com
September 16, 2022
regex - Java remove all non alphanumeric character from beginning and end of string - Stack Overflow
1 Java Regex to replace string surrounded by non alphanumeric characters · 37 Java regular expression to remove all non alphanumeric characters EXCEPT spaces More on stackoverflow.com
🌐 stackoverflow.com
regex - Java regular expression to remove all non alphanumeric characters EXCEPT spaces - Stack Overflow
I'm trying to write a regular ... all non-alphanumeric characters from a paragraph, except the spaces between the words. ... However, the compiler gave me an error message pointing to the s saying it's an illegal escape character. The program compiled OK before I added the \s to the end of the regular expression, but the problem with that was that the spaces between words in the paragraph were stripped out. ... Java will interpret \s as a Java String escape character, ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to remove all non-alphanumeric characters from a string in java?
Remove Non-Alphanumeric Characters from a String in Java
December 19, 2024 - ... // check if the current character ... ascii<=122) || (ascii>=48 && ascii<=57))) ... In this approach, we use the replaceAll() method in the Java String class....
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › remove-non-alphanumeric-characters-java-string
Java alphanumeric patterns: How to remove non-alphanumeric characters from a Java String | alvinalexander.com
April 18, 2019 - As you can see, this example program creates a String with all sorts of different characters in it, then uses the replaceAll method to strip all the characters out of the String other than the patterns a-zA-Z0-9. The code essentially deletes every other character it finds in the string, leaving only the alphanumeric characters. package foo; public class StringTest { public static void main(String[] args) { String s = "yo-dude: like, ... []{}this is a string"; s = s.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(s); } } ... alvinalexander.com is owned and operated by Valley Programming, LLC In regards to links to Amazon.com, As an Amazon Associate I (Valley Programming, LLC) earn from qualifying purchases This website uses cookies: learn more
🌐
w3resource
w3resource.com › java-exercises › re › java-re-exercise-21.php
Java - Remove all non-alphanumeric characters from a string
March 16, 2026 - public class test { public static void main(String[] args) { String text ="Java Exercises"; System.out.println("Original string: "+text); System.out.println("After removing all non-alphanumeric characters from the said string: "+validate(text)); text ="Ex@^%&%(ercis*&)&es"; System.out.println("\nOriginal string: "+text); System.out.println("After removing all non-alphanumeric characters from the said string: "+validate(text)); } public static String validate(String text) { return text.replaceAll("[^a-zA-Z]", ""); } }
🌐
Baeldung
baeldung.com › home › java › java array › removing all non-alphabetic characters from string array in java
Removing All Non-alphabetic Characters From String Array in Java | Baeldung
November 18, 2024 - They let us define patterns for ... both flexible and easy to implement: public static String removeNonAlphabeticUsingRegex(String input) { return input.replaceAll("[^a-zA-Z]", ""); }...
🌐
Quora
quora.com › How-can-I-remove-all-non-alphanumeric-characters-from-a-string-in-Java
How to remove all non-alphanumeric characters from a string in Java - Quora
Answer: Here you go! Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), ...
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › trying to remove non-alphabetical characters from inputted string need help pls
r/learnprogramming on Reddit: Trying to remove non-alphabetical characters from inputted string need help pls
February 22, 2024 -

Hey guys! For class I'm trying to make a program to remove all non-alpha characters from a inputted string. Can you tell me why this isn't working?

For some reason when I input for example "testing test ! bla -o-" it outputs "testingtest!bla-o"

#include <iostream>
using namespace std;
int main() {

string inputStr;
getline(cin, inputStr);
for (int i = 0; i < inputStr.size(); i++){
if (!isalpha(inputStr[i])){
inputStr.replace(i, 1, "");
}
}
cout << inputStr;
return 0;
}

🌐
Chrisnewland
chrisnewland.com › java-remove-non-alphanumeric-characters-from-string-114
Java remove non alphanumeric characters from String
££$$%%##"; String onlyAlphaNumeric = userInput.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(onlyAlphaNumeric); ... At 2018-11-06 15:11:00, JAntonio left the following comment ... Gret and useful examples..thanks! ... Cascading OpenJDK builds Quickly find worst GC pauses in G1 and Parallel GC logs Identify the Java Thread taking the most CPU Run an IBM J9 VM in a docker container DemoFX Part 3 More Bytecode Geekery with JarScan DemoFX Part 2 OpenJFX Nightly Builds for Linux amd64 and armv6hf for Raspberry Pi DemoFX Part 1 Add JavaFX support to Azul Systems' Zulu JDK using OpenJFX JarSca
🌐
Experts Exchange
experts-exchange.com › questions › 29246681 › How-to-remove-non-alphanumeric-characters-in-Java.html
Solved: How to remove non-alphanumeric characters in Java? | Experts Exchange
September 16, 2022 - public String removeNonAlphanumericCharacters(String source) { return source.codePoints() .filter(Character::isLetterOrDigit) .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); } ... EARN REWARDS FOR ASKING, ANSWERING, AND MORE. Earn free swag for participating on the platform. ... Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries.
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-all-non-alphabetical-characters-of-a-string-in-java
Remove all non-alphabetical characters of a String in Java - GeeksforGeeks
March 24, 2023 - In this approach, we use the replaceAll() method to replace all non-alphabetic characters with an empty string. The regular expression "[^a-zA-Z]" matches any character that is not an English alphabetical letter (both uppercase and lowercase).
🌐
EE-Vibes
eevibes.com › computing › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - EE-Vibes
December 9, 2021 - For example, replacing all non-alphanumeric characters in "a*c!" with 1 results in "a1c1" Call re.sub(pattern, repl, string) with pattern as "[^0-9a-zA-Z]+" to replace every non-alphanumeric character with repl in string.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Remove Non-alphabetic Characters From String Array Example - Java Code Geeks
December 30, 2024 - In this step, I will create a RemoveNonAlphabeticUtil.java class that includes four methods to remove non-alphabetic characters. viaCharacter utilizes the StringBuilder to append the alphabetic characters from the character in toCharArray.
🌐
TutorialsPoint
tutorialspoint.com › article › remove-all-non-alphabetical-characters-of-a-string-in-java
Remove all non-alphabetical characters of a String in Java?
April 22, 2025 - When you are working with strings in Java, you may want to keep only the alphabetic characters (A-Z, a-z) and remove everything else, like numbers, punctuation, and symbols. Java gives you multiple ways to do this for different cases or scenarios.
🌐
Coderanch
coderanch.com › t › 371794 › java › Efficient-Remove-Alphanumerics-String
Efficient Way To Remove Non Alphanumerics From A String (Java in General forum at Coderanch)
September 18, 2003 - In 1.4 you can use regular expressions to filter the string. I'm no wizard with them, so someone else will have to come up with the actual expression to use ... I dont know much about Regular expressions, but if you requirement is specific (removing non-alphanumeric), I suggest a custom code, because nothing can be more efficient than a specialized code.
🌐
Brainly
brainly.com › computers and technology › high school › how can we ignore all non-alphanumeric characters in java?
[FREE] How can we ignore all non-alphanumeric characters in Java? - brainly.com
To ignore all non-alphanumeric characters in Java, use the String.replaceAll() method with regex pattern [^a-zA-Z0-9], which effectively removes any character that is not a letter or a number from a given string, useful for cleaning data like ...
🌐
Brainly
brainly.in › computer science › secondary school
wap to remove alphanumeric characters from a string in java using scanner.​ - Brainly.in
May 26, 2021 - It can be punctuation characters ... dollar sign($), equal symbol(=), plus sign(+), apostrophes(‘). The approach is to use the String.replaceAll method to replace all the non-alphanumeric characters with an empty ...
🌐
CodingTechRoom
codingtechroom.com › question › -remove-non-alphanumeric-characters
How to Remove Non-Alphanumeric Characters in Strings? - CodingTechRoom
This task can be achieved using various techniques across different languages, specifically through regular expressions, string manipulation functions, or libraries. ... function removeNonAlphanumeric(str) { return str.replace(/[^a-zA-Z0-9]/g, ''); } // Example usage: removeNonAlphanumeric('Hello, World! 123'); // Output: 'HelloWorld123' ... Mistake: Not accounting for Unicode characters which may be alphanumeric in some contexts.