java - How to change utf-8mb4 to UTF-8 in groovy? -


currently, have problem receiving string input user mobile. string inputed , contained utf8mb4 characters (smiley, emoji, etc). caused error in backend (mysql) since accepts utf-8 input.

now, how can replace utf-8mb4 input utf-8?

def utf8mb4string = '👳👳👳👳👳👳👳'; // parse utf8mb4string utf8 // logic here //possible utf8 result maybe: '�������'  

i have found similar question here how convert utf-8mb4 utf-8? no clear answer yet implementation in groovy.

you can't store characters (like "man turban") outside basic multi-lingual plane (bmp) mysql's poorly-named "utf8" encoding. need specify "utf8mb4" instead.

if don't care store characters, , want replace or discard them, you'd have iterate on string, , build new string (in java):

intstream converted = utf8mb4string.codepoints().map(cp -> character.isbmpcodepoint(cp) ? cp : '\ufffd'); string str = converted.collect(stringbuilder::new, (buf, ch) -> buf.append((char) ch), stringbuilder::append).tostring(); 

or, in groovy syntax:

def transform = { string ->   char ch = it.charat(0)   if (character.ishighsurrogate(ch))     return '\ufffd'   else if (character.islowsurrogate(ch))     return ''   else     return it; } utf8mb4string.collect(transform).join() 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -