0

I have a problem with Substring function. As you can see on Watch window shcreenshot I have the variable called val equal to ‎03.‎09.‎2015 ‏‎17:30

I do not understand why but

val.Substring(0,2) returns 0 instead of 03
val.Substring(0,3) returns 03 (string of two symbols)

What am I doing wrong?

  • Have you clicked on the "reload" button for both of the expression? – Martheen Apr 06 '21 at 11:22
  • 1
    Btw, you should not use string methods to parse this `Date` but `Date.Parse`/`Date.ParseExact`/`Date.TryParse`/`Date.TryParseExact`. Then you have all you need(for example the date, hours, minutes etc.) – Tim Schmelter Apr 06 '21 at 11:26
  • Yes, I reloaded expressions – Sergey Fedorenko Apr 06 '21 at 11:27
  • 4
    Upon pasting your value to dotnet fiddle, turns out you have invisible characters there https://dotnetfiddle.net/ZEsy3n – Martheen Apr 06 '21 at 11:28
  • The original problem was with Parse (and Parse-like) functions. It can not parse this string as Date or DateTime – Sergey Fedorenko Apr 06 '21 at 11:28
  • You can remove the nonprintable character https://stackoverflow.com/questions/15259275/removing-hidden-characters-from-within-strings so it can be parsed by DateTime parser – Martheen Apr 06 '21 at 11:30

2 Answers2

3

Your string contains non-printable characters. Note the following from your screenshot:

val = "03.09.2015 17:30"
val.Length = 21

However, 03.09.2015 17:30 only has 16 characters. Thus, the string contains other, zero-width characters.

To find the culprit, output a hex dump of your problematic string and compare it with the hex dump of the literal string 03.09.2015 17:30.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

From OP comment:

The original problem was with Parse (and Parse-like) functions. It can not parse this string as Date or DateTime

Once the invisible characters are removed from the string, it certainly can be parsed:

Dim d As Date = DateTime.Parse("03.09.2015 17:30", Globalization.CultureInfo.InvariantCulture)
Dim m As Integer = d.Month    ' m = 3

Of course, choose the particular Parse method that best fits your needs.

MarkL
  • 1,721
  • 1
  • 11
  • 22