Reputation: 25370
I have the simplest VB code:
Dim test As String = "DDN8057"
Console.WriteLine(test.TrimStart("DDN"))
gives me
N8057
Why? Converting this to C# (which I'm far more familiar with), made me realize that TrimStart
actually expects a params char[]
, but running
Console.WriteLine("DDN8057".TrimStart("DDN".ToCharArray()));
gives me my expected
8057
So, I guess VB is capable of treating a string as a char array internally (is this true?), but why the discrepancy in my output?
Upvotes: 2
Views: 1727
Reputation: 27322
You do not have Option Strict switched On in your VB project.
I can tell because test.TrimStart("DDN")
does not compile when this is on. This is because as you correctly pointed out TrimStart
expects an explicit char array (or a single char)
What happens when you run this with Option Strict Off is the compiler coerces the String
(DDN
) into a single char (D
) (this is an implicit narrowing conversion which Option Strict expressly forbids) which is why you get N8057
as your output.
You would think that as a string is just a char array it would convert it to an array but it doesn't - it effectively performs CChar("DDN")
!
Conclusion
Option Strict On = Good. Here is how to switch it on by default: Option Strict on by default in VB.NET
Upvotes: 3