Removing Invisible Unicode Characters from Strings in C#
Background Recently I found that in data pulled down from a data collection system, the JSON in some fields was inexplicably truncated, causing JSON parsing to fail during subsequent data analysis. Something like this {title: 你好 Or like this, with an extra double quote or something {title:你好} Because the database is Oracle, I initially thought this old relic Oracle was the problem. After some fussing around, I pulled out the SQL statement for every write, and the JSON format inside all looked fine. This is way too weird. It looks fine, but then why is the JSON
目录4 个章节
Background
Recently I found that in data pulled down from a data collection system, the JSON in some fields was inexplicably truncated, causing JSON parsing to fail during subsequent data analysis.
Something like this
{"title": "你好
Or like this, with an extra double quote or something
{"title":""你好"}
Because the database is Oracle, I initially thought this old relic Oracle was the problem. After some fussing around, I pulled out the SQL statement for every write, and the JSON format inside all looked fine.
This is way too weird. It looks fine, so why was the JSON randomly truncated?
Finally I tried putting the entire SQL into Rider's query console and executing it, then reading this JSON back from the database, and surprisingly found it had become this:
{"title":"?你好"}
Ah, seeing this big question mark, I immediately knew that this “你好” was more than just these two characters—it definitely contained invisible Unicode characters.
Then I copied this JSON out, opened it in hex mode, and sure enough saw a \u0020 character in front of “你好”…
Unicode Code Charts
- 0000-007F: C0 Control Characters and Basic Latin (C0 Control and Basic Latin)
- 0080-00FF: C1 Control Characters and Latin-1 Supplement (C1 Control and Latin 1 Supplement)
- 0100-017F: Latin Extended-A (Latin Extended-A)
- 0180-024F: Latin Extended-B (Latin Extended-B)
- 0250-02AF: IPA Extensions (IPA Extensions)
- 02B0-02FF: Spacing Modifier Letters (Spacing Modifiers)
- ……
Here I’m also attaching part of the Unicode table
| U+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0000 | NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | HT | LF | VT | FF | CR | SO | SI |
| 0010 | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM | SUB | ESC | FS | GS | RS | US |
| 0020 | ! | " | # | $ | % | & | ' | ( | ) | * | + | , | - | . | / | |
| 0030 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | < | = | > | ? |
| 0040 | @ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
| 0050 | P | Q | R | S | T | U | V | W | X | Y | Z | [ | \ | ] | ^ | _ |
| 0060 | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |
You can see that \u0020 above is in the third row, first column—it’s an invisible character, hiding in front of the title
It was because of this Unicode character that Oracle couldn’t parse it correctly, which caused things to get messed up when inserting data
So the case is solved: the system’s front-end users somehow got a Unicode character in there when typing…
The fix is for me to do another round of filtering when collecting on my side…
Didn’t expect filtering Unicode in C# to be such a hassle—there’s so little material…
In the end I still put it together by referring to Java materials. = =...
Code
The code is as follows
I wrote an extension method to filter
public static class StringExt {
// 控制字符
private static readonly Regex ControlCharRegex = new Regex(@"[\p{C}]", RegexOptions.Compiled);
/// <summary>
/// 移除控制字符
/// </summary>
public static string RemoveControlChars(this string text) {
return ControlCharRegex.Replace(text, string.Empty);
}
}
When you need to use it, do it like this
var outStr = "带有Unicode的字符串".RemoveControlChars();
Done.
References
- Unicode Code Chart and Some Invisible Character Filtering Approaches - https://www.cnblogs.com/fan-yuan/p/8176886.html
- https://stackoverflow.com/questions/6198986/how-can-i-replace-non-printable-unicode-characters-in-java
_EOF_
Removing Invisible Unicode Characters from Strings in C#
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
C#实现解析Markdown文档并处理图片链接
前言 又是好久没写博客了 其实也不是没写,是最近在「做一个博客」,从2月21日开始,大概一个多星期的时间,疯狂刷进度,边写代码边写了一整系列的博客开发笔记,目前为止已经写了16篇了,然后上3月之后工作有点忙~~加上有点沉迷原神~~,然后进度就暂时慢下来了 事实证明做什么事情一旦停下来就会产生惰性,于是停了这么久,我终于下定决心继续更新! 这次开发的博客用的是C#语言,搭配.netcore技术栈;前
2022-03-19 · C# · 约 1 分钟
开发现代化的.NetCore控制台程序:(4)使用GithubAction自动构建以及发布nuget包
GitHub Action自动构建发布nuget包,省时省力。Secrets管理token,workflow一劳永逸。吐槽nuget推送重复输入token,烦人!
2023-11-13 · C# · 约 1 分钟
在.NetCore(C#)中使用ODP-NET-Core+Dapper操作Oracle数据库
前言 虽然一直在说“去IOE化”,但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的…… 我们的系统主要的技术栈是Django / Spring / AspNetCore,Java的不必说对Oracle支持肯定没问题,关键在于Django对Oracle版本有要求,兼容性不是特别好,Oracle版本没办法随意升级的,所以我想到用.Net
2021-12-18 · C# · 约 1 分钟
SharpIco:用纯C#打造零依赖的.ico图标生成器,支持.NET9与AOT编译
SharpIco 是一款纯 C# 实现的跨平台图标生成工具,支持 AOT 编译,无需外部依赖。它能将 PNG 图片分解为多尺寸(16x16至512x512)合成 ICO 图标,并支持图标结构分析。工具提供命令行界面,可通过 dotnet tool 一键安装,并已发布至 NuGet 和 Scoop。项目旨在简化图标生成与验证,追求轻量、纯粹、易集成的使用体验。
2025-05-26 · C# · 约 1 分钟
讨论
共 0 条加载评论中…