如何從ruby字符串中刪除非UTF8字符?我有一個字符串,例如「xC2」。我想從字符串中刪除該字符,以使其成爲有效的UTF8。從Ruby中的字符串中刪除非UTF字符?
此:
text.gsub!(/\xC2/, '')
返回一個錯誤:
incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
我看着text.unpack( 'U *')和string.pack爲好,但沒有取得任何進展。
你可能會發現http://stackoverflow.com/questions/11375342/stringencode-not-fixing-invalid-byte-sequence-in-utf-8-error/有用 –