Peter
Peter

Reputation: 31

How to replace all non-letters in a given string s by symbol '*'

For example Input

Hello 1/(¤

Output should be

Hello ****

Upvotes: 3

Views: 2176

Answers (2)

Aly
Aly

Reputation: 16265

"Hello 1/(¤".replaceAll("[^A-Za-z\\s]", "*")

Upvotes: 2

Fabian Steeg
Fabian Steeg

Reputation: 45744

To make your regular expression work with international alphabets (e.g. to treat letters with diacritics as letters too, like ä, à, etc.), you should use the unicode-aware expression for a non-letter character:

"Héllö 1/(¤".replaceAll("[^\\p{L}\\s]", "*");

Upvotes: 7

Related Questions