gpt4 book ai didi

c# - 解析字符串时出现奇怪的错误?

转载 作者:太空狗 更新时间:2023-10-29 20:52:57 25 4
gpt4 key购买 nike

当我尝试像这样解析日期时:

DateTime t1 = DateTime.ParseExact("August 11, 2013, 11:00:00 PM", "MMMM dd, yyyy, hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

它工作正常,但是当我这样做时:

string s ="‎August ‎11, ‎2013, ‏‎11:00:00 PM";
DateTime t = DateTime.ParseExact(s, "MMMM dd, yyyy, hh:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);

我收到这个错误:

An exception of type 'System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code

最佳答案

因为你的字符串

string s = "‎August ‎11, ‎2013, ‏‎11:00:00 PM";

包括0x200e(8206) character8 月 的开始和结束。你可以很容易地看到它

var chars = s.ToCharArray();

好像是复制+粘贴的问题

您可以通过以下方式删除这些字符:

var newstr = new string(s.Where(c => c <128).ToArray())

关于c# - 解析字符串时出现奇怪的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com