The tr shell replaces one character set with another character set. For example, echo hello | tr [az] [AZ] echo hello | tr [az] [AZ] will broadcast hello to hello .
In java, however, I have to replace each character separately, as shown below.
"10 Dogs Are Racing" .replaceAll ("0", "0") .replaceAll ("1", "1") .replaceAll ("2", "2") // ... .replaceAll ("9", "9") .replaceAll ("A", "A") // ... ;
The apache-commons-lang library provides a convenient replaceChars method for such a replacement.
// half-width to full-width System.out.println ( org.apache.commons.lang.StringUtils.replaceChars ( "10 Dogs Are Racing", "0123456789ABCDEFEGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "0123456789ABCDEFEGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ) ); // Result: // 10 Dogs Are Racing
But, as you can see, once the searchCards / replaceChars are too long (also too boring if you want, duplicate the character) and can be expressed with a simple regular expression [0-9A-Za-z] / [0-9A-Za-z] Is there a regular expression way to achieve this?
java regex replace
LiuYan 刘 研
source share