C# 解析到日期的字符串时出现奇怪错误?
当我尝试像这样解析日期时:C# 解析到日期的字符串时出现奇怪错误?,c#,.net,datetime,windows-phone,C#,.net,Datetime,Windows Phone,当我尝试像这样解析日期时: 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.Par
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);
我得到这个错误:
中发生“System.FormatException”类型的异常
mscorlib.ni.dll,但未在用户代码中处理
因为你的绳子
string s = "August 11, 2013, 11:00:00 PM";
包括在8月初和8月底。你可以很容易地看到它
var chars = s.ToCharArray();
似乎是复制+粘贴问题
您可以通过以下方式删除这些字符:
var newstr=newstring(s.Where(c=>cHaha,我找到了
首先,这两个代码都没有问题。它们都可以正常工作。只是字符串不相等。第二个代码中有一些隐藏字符
您的第一个“2013年8月11日,晚上11:00:00”。长度为28
但是第二个“八月11, 2013, “晚上11:00:00”,长度为33
让我们试试这个代码
string s = "August 11, 2013, 11:00:00 PM";
string s1 = "August 11, 2013, 11:00:00 PM";
char[] c = s.ToCharArray();
char[] c1 = s1.ToCharArray();
foreach (var ch in c)
{
Console.WriteLine(ch);
}
foreach (var ch1 in c1)
{
Console.WriteLine(ch1);
}
输出将是
A
u
g
u
s
t
1
1
,
2
0
1
3
,
1
1
:
0
0
:
0
0
P
M
? // <-- What the hell?
A
u
g
u
s
t
? // <-- What the hell?
1
1
,
? // <-- What the hell?
2
0
1
3
,
? // <-- What the hell?
? // <-- What the hell?
1
1
:
0
0
:
0
0
P
M
A
U
G
U
s
T
1.
1.
,
2.
0
1.
3.
,
1.
1.
:
0
0
:
0
0
P
M
?//第二个字符串具有隐藏字符
运行以下命令:
string s1 = "August 11, 2013, 11:00:00 PM";
string s2 = "August 11, 2013, 11:00:00 PM";
Console.WriteLine(s1.Length); // 28
Console.WriteLine(s2.Length); // 33
具体来说,作为字符数组,第二个是:
s2.ToCharArray();
{char[33]}
[0]: 8206 '' // ????
[1]: 65 'A'
[2]: 117 'u'
[3]: 103 'g'
[4]: 117 'u'
[5]: 115 's'
[6]: 116 't'
[7]: 32 ' '
[8]: 8206 '' // ????
[9]: 49 '1'
[10]: 49 '1'
[11]: 44 ','
[12]: 32 ' '
[13]: 8206 '' // ????
[14]: 50 '2'
[15]: 48 '0'
[16]: 49 '1'
[17]: 51 '3'
[18]: 44 ','
[19]: 32 ' '
[20]: 8207 '' // ????
[21]: 8206 '' // ????
[22]: 49 '1'
[23]: 49 '1'
[24]: 58 ':'
[25]: 48 '0'
[26]: 48 '0'
[27]: 58 ':'
[28]: 48 '0'
[29]: 48 '0'
[30]: 32 ' '
[31]: 80 'P'
[32]: 77 'M'
我也遇到了这个问题。在我的例子中,UI自动化测试失败了,因为IE似乎自动添加了这个LRM(从左到右)标记(Firefox和Chrome没有)。一行快速的代码将其删除:
Regex.Replace(date, @"\u200e", string.Empty)
既然我已经从模型中的sting中获得了它,那么我如何才能删除它呢?@ahmedmadvar newstr=newstring(s1.Where(c=>c@Ahmedmad存在从左到右和从右到左标记的原因可能是字符串来自于一些带有英文月名的阿拉伯文本,其中包含从左到右的有序数字组。您可以删除答案中的非ASCII字符,但如果它们总是在同一位置,为什么不直接解析它们,如DateTime.ParseExact(s,“\u200emmm\u200Edd,\u200Eyyyy,\u200F\u200Ehh:mm:ss tt”,CultureInfo.InvariantCulture)
@Downvoter是否愿意评论一下,这样我就可以知道我可能错在哪里了?你意识到你每次都拼错了长度
,对吗?:@SmartDev-Ups,7年的错误,修复了(:
Regex.Replace(date, @"\u200e", string.Empty)