简体   繁体   中英

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

我想用下面的字符串对象BusDetails中的所有特殊字符替换除了_(下划线)和java中的数字之外的空白“”?

BusDetails=BusDetails.replaceAll("—", "").replaceAll("\\s+","_").replaceAll("ROUTE", "BUS").replaceAll("-", "_");

This should fix it:

BusDetails=BusDetails.replaceAll("(\\W|^_)*", "");

The pattern (\\\\W|^_) matches any non-word character. Additionally it excludes _ .

BusDetails=BusDetails.replaceAll("[^_0-9]+", "");

这保留整数但不是小数(为此添加“。”)

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

使用正则表达式模式"[^a-zA-Z0-9_-]"我们可以替换字符串中除字母,数字和“_”之外的所有特殊字符(符号)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM