I have a problem with string.split using.NET 3.5:
String to split is:
dim source as string = "ab|foo|bar|bar|bar-foo|ab|ezrezertr|ghghhjhj|ab|foo|xxx|"
dim result() as string = source.split("ab|")
When used within a Winforms applicaton, the result is “correct”:
result(0) is “foo|bar|bar|bar-foo|”
result(1) is “ezrezertr|ghghhjhj|”
result(2) is “foo|xxx|”
And I’m happy!
When used within an ASP.NET code behind, the result is:
result(0) is “b|foo|bar|bar|bar-foo|”
result(1) is “b|ezrezertr|ghghhjhj|”
result(2) is “b|foo|xxx|”
In other words, the split function only get rid of the 1st character of the separator string!
Does someone know why?
The ASP.NET results look like you are using
Regex.Splitrather thanString.Split. The string"ab|"will be interpreted as a regular expression for “a” followed by “b” or nothing, so just an “a” matches.Later: Second Theory:
String.Splitthat takes a single string argument. The only single argument overload takes an array ofchar.String.Splt(char())will split on any of the passed characters.Option Strict Onwill implicitly convert a string to an array of chars.Hence I think in the ASP.NET case you don’t have
option strict on, thereforetheString.Split(anotherString)is being treated astheString.Split(anotherString.ToCharArray()).Thus splitting on just a
"b".However this leaves the question of how the first cases acts as passing a string, but there is no overload taking a
String()without extra parameters (aSplitOptions)…Summary: Visual Basic’s extra implicit conversions and behaviour set at a file/project/language level can make identical code behave differently.