我在UTF8中有一个文本,我想将其转换为iso-8859-1。另外,我有一个iso-8859-1格式的文本,我想编码成UTF8。
我认为使用本机函数是不可能的,那么有没有好的库可以做到这一点呢?我在浏览器上用的是纯javascript,不是nodejs,也不是用的是webpack等等。
谢谢。
发布于 2020-04-24 09:14:17
除了escape
之外,还需要使用decodeURIComponent
才能在UTF8/ ISO-8859-1之间来回切换
有关它的更多信息,请参阅this article。
解码UTF-:fixedstring = decodeURIComponent(escape(utfstring));
解码ISO-8859-1:utfstring = unescape(encodeURIComponent(originalstring));
要查看字符串是否为UTF-8,请执行以下操作:
var fixedstring;
try{
// If the string is UTF-8, this will work and not throw an error.
fixedstring=decodeURIComponent(escape(badstring));
}catch(e){
// If it isn't, an error will be thrown, and we can assume that we have an ISO string.
fixedstring=badstring;
}
https://stackoverflow.com/questions/61404481
复制相似问题