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 kno开发者_如何转开发w why?
The ASP.NET results look like you are using Regex.Split
rather than String.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:
- There is no overload of
String.Split
that 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.- VB, without
Option Strict On
will implicitly convert a string to an array of chars.
Hence I think in the ASP.NET case you don't have option strict on
, therefore theString.Split(anotherString)
is being treated as theString.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 (a SplitOptions
)...
Summary: Visual Basic's extra implicit conversions and behaviour set at a file/project/language level can make identical code behave differently.
If the input string is the same, then I can nearly guarantee you that the split method of a Console app and ASP.NET behave the same.
Maybe the output is not displayed correctly?
精彩评论