To find out if a string has only printable characters, Python has a function called string.printable. We can make our own String.isPrintable()
using metaprogramming in Groovy.
import java.nio.charset.Charset
String.metaClass.isPrintable = { return Charset.forName("US-ASCII")
.newEncoder()
.canEncode(delegate)
}
assert 'Abc'.isPrintable() == true
assert 'àé'.isPrintable() == false
assert 'môre'.isPrintable() == false
assert 'de076'.isPrintable() == true
Top comments (0)