hutool.core.util.escape¶
Module Contents¶
Classes¶
转义工具类,对应 Java cn.hutool.core.util.EscapeUtil |
API¶
- class hutool.core.util.escape.EscapeUtil¶
转义工具类,对应 Java cn.hutool.core.util.EscapeUtil
- _HTML_ESCAPE_MAP = None¶
- _HTML_UNESCAPE_MAP = None¶
- static escape_html(content: str) str¶
HTML转义,将 & < > “ ‘ 转义为HTML实体
- 参数:
content – 待转义的字符串
- 返回:
转义后的字符串
- static unescape_html(content: str) str¶
HTML反转义,将HTML实体还原为原始字符
- 参数:
content – 待反转义的字符串
- 返回:
反转义后的字符串
- static escape_xml(content: str) str¶
XML转义(同HTML转义)
- 参数:
content – 待转义的字符串
- 返回:
转义后的字符串
- static unescape_xml(content: str) str¶
XML反转义(同HTML反转义)
- 参数:
content – 待反转义的字符串
- 返回:
反转义后的字符串
- static escape_sql(content: str) str¶
SQL转义,将单引号转为两个单引号
- 参数:
content – 待转义的字符串
- 返回:
转义后的字符串
- static escape_url(url: str, charset: str = 'utf-8') str¶
URL编码
- 参数:
url – 待编码的URL字符串
charset – 字符编码,默认utf-8
- 返回:
编码后的URL字符串
- static unescape_url(url: str, charset: str = 'utf-8') str¶
URL解码
- 参数:
url – 待解码的URL字符串
charset – 字符编码,默认utf-8
- 返回:
解码后的URL字符串
- static unescape_html_chars(content: str) str¶
将常用HTML实体(< > & " ')还原为原始字符。
与
unescape_html()的区别在于本方法仅处理 5 个基本实体, 不使用html.unescape(后者会处理所有 HTML5 实体)。Examples:
"<p>Hello & World</p>" -> "<p>Hello & World</p>"
- 参数:
content – 包含HTML实体的字符串
- 返回:
还原后的字符串
- static escape_all(content: str) str¶
转义所有非字母数字字符为 Unicode 转义序列
\uXXXX。- 参数:
content – 待转义的字符串
- 返回:
转义后的字符串
- static safe_unescape(content: str) str¶
安全反转义,失败时返回原串。
尝试反转义 Unicode 转义序列(
\uXXXX),如果解析失败则返回原始字符串。- 参数:
content – 待反转义的字符串
- 返回:
反转义后的字符串,失败返回原串
- static unescape(content: str) str¶
反转义 Unicode 转义序列(
\uXXXX)。- 参数:
content – 包含
\uXXXX转义序列的字符串- 返回:
反转义后的字符串
- 抛出:
ValueError – 转义序列格式非法时