Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all special characters except for underscore and numbers in java?

Tags:

java

I want to replace all special characters from the string object BusDetails below with a blank "" except for _(underscore) and numbers in java ?

BusDetails=BusDetails.replaceAll("—", "").replaceAll("\\s+","_").replaceAll("ROUTE", "BUS").replaceAll("-", "_");
like image 319
dev_marshell08 Avatar asked Oct 02 '22 09:10

dev_marshell08


1 Answers

BusDetails = BusDetails.replaceAll("[^a-zA-Z0-9_-]", "");

Using the regex pattern "[^a-zA-Z0-9_-]" we can replace all special characters (symbols) from the string except letters, numbers and '_'.

like image 134
Amol Birajdar Avatar answered Oct 05 '22 11:10

Amol Birajdar