Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having an issue with converting foreign names to upper/lower case... It seems to omit foreign characters. Any idea how I could solve this?

String str = "SéBASTIEN DOL DE BRETAGNE";
return str.split(/[^w]/).collect { it.toLowerCase().capitalize() }.join(" ");

Current output: S Bastien Dol De Bretagne
Desired output: Sébastien Dol De Bretagne

I would like to have it work with all foreign characters (Swedish, French etc) but I'm at a lost :-)

question from:https://stackoverflow.com/questions/65916303/include-foreign-characters-in-groovy-split-collect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're splitting on non-word characters, and that's the problem. w is [a-zA-Z_0-9] (See the Pattern class's JavaDocs), which does not include é. That explains why this character, in either case, is being used as a delimiter.

You could split by spaces or blanks:

//any version of these, specifying the delimiter
s.split(/ +/).collect { it.toLowerCase().capitalize() }.join(" ")
s.split(/s+/).collect { it.toLowerCase().capitalize() }.join(" ")

Both of the above return "Sébastien Dol De Bretagne"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...