Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
import java.nio.ByteBuffer
import java.nio.charset.CharacterCodingException
import static java.nio.charset.StandardCharsets.UTF_8
final decoder = UTF_8.newDecoder()
final buffer = ByteBuffer.wrap(s)
try {
decoder.decode(buffer)
b = true
} catch (CharacterCodingException e) {
b = false
}
Decoders are not thread-safe.
This is @CompileStatic compatible.
This is @CompileStatic compatible.
try:
s.decode('utf8')
b = True
except UnicodeError:
b = False
b = s.force_encoding("UTF-8").valid_encoding?
example: s = "\xc2"