简体   繁体   English

Ruby / Rails:如何删除字符串中的所有unicode?

[英]Ruby/Rails : How to remove all the unicode in a string?

I have a lot of records in a legacy database and I need to export that data into a CSV using the ISO-1889-1 format because there are spanish characters. 我在旧版数据库中有很多记录,并且由于存在西班牙语字符,因此需要使用ISO-1889-1格式将该数据导出为CSV。

However, when converting it to utf-8 to iso, it keeps throwing errors of not being able to convert certain characters. 但是,将其从utf-8转换为iso时,会一直抛出无法转换某些字符的错误。 ex: 例如:

Encoding::UndefinedConversionError - U+2026 from UTF-8 to ISO-8859-1

this happens at this line in my controller: 这发生在我的控制器的这一行:

send_data(data.encode("iso-8859-1"), filename: "books_data_#{date}.csv", type: 'text/csv; charset=iso-8859-1; header=present')

To fix this individual issue I just did string.gsub!("…", "") . 为了解决这个单独的问题,我只做了string.gsub!("…", "") Is there a more universal way to just remove all unicode in a ruby string? 有没有更通用的方法来删除ruby字符串中的所有unicode? Doing it by hand for each one that appears is not as complete, ugly, and hard to maintain if new unicode characters arise. 如果出现新的unicode字符,则对出现的每个字符进行手工操作并不那么完整,丑陋且难以维护。 Just wondering. 就是想。

Are you looking for String::encode ? 您在寻找String :: encode吗?

irb(main):011:0> "Здравствуйте Stack Overflow!".encode("iso-8859-1", undef: :replace, replace: "")
=> " Stack Overflow!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM