如何在 Haskell 中将 Unicode 转义序列转换为 Unicode 字符串 [英] How to convert Unicode Escape Sequence to Unicode String in Haskell

查看:32
本文介绍了如何在 Haskell 中将 Unicode 转义序列转换为 Unicode 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像 "36193657363436093648359236573648362136573591" 这样的字符串,我想对其进行解码.我尝试搜索 unicode 库但没有成功.

I have a string like "36193657363436093648359236573648362136573591" which I want to decode it. I tried search the unicode library without success.

推荐答案

前奏> putStrLn "36193657363436093648359236573648362136573591"
้านเจ้เล้ง

Prelude> putStrLn "36193657363436093648359236573648362136573591"
ร้านเจ้เล้ง

请注意,您实际上没有字符串 "36193657363436093648359236573648362136573591" –相反,您有 UTF-32 字符串 ร้านเจ้เล้ง,其中 "36193657..." 恰好是符合 ASCII 的文字.默认情况下,GHCi 使用 Show 实例来显示结果,它不会显示事物,而是吐出可以用作事物的 Haskell 代码的文字.它在 unicode 方面是保守的.这就是为什么

Note that you don't actually have the string "36193657363436093648359236573648362136573591" – rather, you have the UTF-32 string ร้านเจ้เล้ง, for which "36193657..." happens to be a ASCII-compliant literal. By default, GHCi uses the Show instance to display results, which doesn't so much show things as spit out literals that can be used as Haskell code for the thing. It's conservative in terms of unicode. That's why

前奏>ร้านเจ้เล้ง"
"36193657363436093648359236573648362136573591"

Prelude> "ร้านเจ้เล้ง"
"36193657363436093648359236573648362136573591"

另一方面,putStrLnputCharhPutStr 等函数只会将字符串本身转储为 UTF-8 而不是其 ASCII 安全表示.

On the other hand, the putStrLn, putChar, hPutStr etc. functions will just dump the string itself in UTF-8 rather than an ASCII-safe representation thereof.

如果您实际上是从文件或其他东西中读取转义字符串,您可以简单地读取它:

If you're actually reading the escaped string from a file or something, you can simply read it:

前奏> s <‌- getLine
"36193657363436093648359236573648362136573591"
前奏> s
""\3619\3657\3634\3609\3648\3592\3657\3648\3621\3657\3591""
-- 注意双重转义,因为我正在显示一个包含字符串文字的字符串.
前奏> putStrLn $ 读s
้านเจ้เล้ง

Prelude> s <‌- getLine
"36193657363436093648359236573648362136573591"
Prelude> s
""\3619\3657\3634\3609\3648\3592\3657\3648\3621\3657\3591""
-- Note double escaping, because I'm showing a string that contains a string literal.
Prelude> putStrLn $ read s
ร้านเจ้เล้ง

这篇关于如何在 Haskell 中将 Unicode 转义序列转换为 Unicode 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆