问号(“?”)附加到字符串

Kai*_*ran -1 c# string unicode strip

所以我正在编写一个程序并使用其他人编写的现有库。他们的库正在调用 TheMovieDatabase.com 并检索有关电影的信息,包括 Youtube 预告片名称,如“sErD7Y00R_8”。

当我调试并查看存储该值的预告片名称字符串变量时,它显示为“sErD7Y00R_8”,但是当它插入我的数据库或打印到控制台时,它似乎附加了一个?(问号)到最后,显示如下:“sErD7Y00R_8?”

这显然给我带来了一些问题。我不明白它为什么这样做以及如何解决它。我只能猜测它是一些非常规文本字符或其他东西,但这只是一个猜测。

这是包装器库的链接: https://github.com/LordMike/TMDbLib/

这是我在包装器库中调用的方法,传入 ID 143049:

TMDbLib.Objects.Movies.Movie tmdbMovie = client.GetMovie(id, MovieMethods.Credits | MovieMethods.Keywords | MovieMethods.Images | MovieMethods.Trailers | MovieMethods.Reviews | MovieMethods.Releases);
Run Code Online (Sandbox Code Playgroud)

这是之后立即打印到控制台的内容:

Console.WriteLine("'" + tmdbMovie.Trailers.Youtube[i].Source + "'");
Run Code Online (Sandbox Code Playgroud)

.Length 属性返回 12,因此它看起来是 1 个字符,它不会在调试器中显示,但会打印为 ? 在控制台中

根据评论,我打印出了 Encoding.GetBytes 详细信息:

Encoding the entire string:
System.Text.UTF7Encoding       : 20  38  :73 45 72 44 37 59 30 30 52 2B 41 46 38 2D 38 2B 49 41 34 2D 
System.Text.UTF8Encoding       : 14  39  :73 45 72 44 37 59 30 30 52 5F 38 E2 80 8E 
System.Text.UnicodeEncoding    : 24  26  :73 00 45 00 72 00 44 00 37 00 59 00 30 00 30 00 52 00 5F 00 38 00 0E 20 
System.Text.UnicodeEncoding    : 24  26  :00 73 00 45 00 72 00 44 00 37 00 59 00 30 00 30 00 52 00 5F 00 38 20 0E 
System.Text.UTF32Encoding      : 48  52  :73 00 00 00 45 00 00 00 72 00 00 00 44 00 00 00 37 00 00 00 59 00 00 00 30 00 00 00 30 00 00 00 52 00 00 00 5F 00 00 00 38 00 00 00 0E 20 00 00 
Run Code Online (Sandbox Code Playgroud)

调试截图

Bea*_*692 5

出现问号似乎是因为编码不匹配,并且由于字符串应该采用 ASCII 编码,因此我们可以删除非 ASCII 字符来解决不匹配问题。

为此,我们可以使用正则表达式查找非 ASCII 字符 ( [^\u0000-\u007F]) 并将其替换为空字符串:

str=Regex.Replace(str, @"[^\u0000-\u007F]", string.Empty);
Run Code Online (Sandbox Code Playgroud)