Strange error parsing a string today?

When I try to parse the date as follows:

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

It works correctly, but when I do this:

 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); 

I get this error:

An exception of type "System.FormatException" occurred in mscorlib.ni.dll, but was not processed in the user code

+6
source share
4 answers

Since your line

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

Includes the 0x200e (8206) character at the beginning and end of August . You can easily see her on

 var chars = s.ToCharArray(); 

This seems to be a problem with copy and paste

You can remove these characters:

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

Haha, I found it.

First of all, there is nothing wrong with both of your codes. Both work great. Only your lines are not equal. There are several hidden characters on your second.

Your first "August 11, 2013, 11:00:00 PM".Lenth - 28

but the second one is "‎August ‎11, ‎2013, ‏‎11:00:00 PM".Lengt is 33

Try this code:

 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); } 

The output will be:

 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 

As a solution, do not copy any line to the line :).

+3
source

The second line contains hidden characters.

Run this:

 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 

In particular, like char arrays, the second is:

 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' 
+2
source

I was also struck. In my case, the UI automation test failed because IE seems to automatically add this LRM label (from left to right) (Firefox and Chrome not). A quick line of code that removes it:

 Regex.Replace(date, @"\u200e", string.Empty) 
0
source

Source: https://habr.com/ru/post/951960/


All Articles