How can I remove non-printable invisible characters from string?
Ruby version: 2.4.1
2.4.1 :209 > product.name.gsub(/[^[:print:]]/,'.')
=> "Kanha"
2.4.1 :210 > product.name.gsub(/[^[:print:]]/,'.').length
=> 6
2.4.1 :212 > product.name.gsub(/[\u0080-\u00ff]/, '').length
=> 6
2.4.1 :214 > product.name.chars.reject { |char| char.ascii_only? and (char.ord < 32 or char.ord == 127) }.join.length
=> 6
2.4.1 :216 > product.name.gsub(/[^[:print:]]/i, '').length
=> 6
The word "Kanha" has 5 letters. However there is a 6th character that is not printable. How can I remove it?
By googling and SOing I have already tried few approaches, but as you can see none of those are helpful.
It is causing problems when I try to integrate out data with other systems.
U+202C
) is considered printable (seeproduct.name.each_char.all?(/[[:print:]]/)
). Do you have issues with other characters? Deleting one character should be easy. – Teaspoonproduct.name
with its value (do that withputs product.name
, cut and paste and add quotes). That way we could figure out what the offending character is, which may lead to a solution. Except for that it's an interesting question. btw, you don't need "Ruby" in the title as it is a tag. – ThrongU+202C
and product.name.each_char.all?(/[[:print:]]/) This gives me error ArgumentError: wrong number of arguments (given 1, expected 0) I dont get what you are trying to say Please do explain – Modicum