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#高性能数组拷贝实验
前言 昨天 wc(Wyu_Cnk) 提了个问题 C# 里多维数组拷贝有没有什么比较优雅的写法? 这不是问对人了吗?正好我最近在搞图像处理,要和内存打交道,我一下就想到了在C#里面直接像C/C++一样做内存拷贝。 优雅?no,要的就是装逼,而且性能还要强🕶 概念 首先澄清一下 C# 里的多维数组 (Multi-dimensional Array) 是这样的 byte[,] arr = new by
2023-01-30 · C# · 约 1 分钟
C# 扩展方法只会写 this 吗?C# 14 新语法直接把扩展方法玩出了花
.NET 10 的扩展方法语法糖?哦,用 `extension` 块定义扩展,支持属性和方法了,以前不行,现在行,真稀奇。静态扩展成员?太晚了点,给 `DateTime` 添加静态方法,终于能优雅了。升级吧,不然怎么享受这些新玩具?
2026-03-05 · C# · 约 1 分钟
方便的C#-Json库:Json-Net
官网 Json.Net用法很简单,本文也只是搬运了一下官网的例子,更多用法可以查看官网,一分钟就能上手。 https://www.newtonsoft.com/json 使用例子 对象序列化 Product product = new Product(); product.Name = Apple; product.Expiry = new DateTime(2008, 12, 28); prod
2022-05-27 · C# · 约 1 分钟
C#爬虫开发小结
前言 2023年以来一直很忙,临近春节,各种琐事更多,但鸽了太久没写文章总是不舒坦,忙中偷闲来记录下最近用C#写爬虫的一些笔记。 爬虫一般都是用Python来写,生态丰富,动态语言开发速度快,调试也很方便 但是 我要说但是,动态语言也有其局限性,笔者作为老爬虫带师,几乎各种语言都搞过,现在这个任务并不复杂,用我最喜欢的C#做小菜一碟~ 开始 之前做 OneCat 项目的时候,最开始的数据采集模块,
2023-01-24 · C# · 约 1 分钟
讨论
共 0 条加载评论中…