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#使用iKvm黑科技无缝接入JVM生态
C#开发者用iKVM白嫖JVM生态?省事,但依赖处理还得手动折腾。无缝接入?不过是把JAR转DLL,省了移植代码的麻烦。AI工具缺?直接调用HanLP,但依赖处理,呵呵。
2023-10-06 · C# · 约 1 分钟
开发现代化的.NetCore控制台程序:(1)项目介绍以及依赖注入/配置/日志等使用细节
开发.NET Core控制台工具,依赖注入和日志整合得比Go优雅多了。哼,配置文件丝滑,但AOT?反射问题,放弃。
2023-11-10 · C# · 约 1 分钟
一次小而美的重构:使用 C# 在 Avalonia 中生成真正好看的词云
重构StarBlogPublisher中词云展示功能的过程。作者对比了两个流行的C#词云库,最终选择了功能更强大的Sdcb.WordCloud。文章详细说明了如何使用该库获取词频数据并生成词云图像,并展示了重构前后的效果对比。
2025-04-26 · C# · 约 1 分钟
试一下用DotNetCore来做机器学习,ML-Net框架跑起来
ML.Net简介 官网:https://dotnet.microsoft.com/apps/machinelearning-ai/ml-dotnet 官方介绍:The ML.NET command line interface (CLI), provides tools for building machine learning models with ML.NET. ML.NET 是面向.NET
2021-12-18 · C# · 约 1 分钟
讨论
共 0 条加载评论中…