I have this code.
string ED= collection["ED"].Replace(string.Empty,"1/1/2011").Split(',').ToString();
when I do this I am geting this error.
String cannot be of zero length.
Parameter name: oldV开发者_StackOverflow中文版alue
is that something I am doig wrong here?
thanks
oldValue is the first parameter of Replace()
so I'm guessing you can't pass string.Empty
to that
The first parameter of the Replace method cannot be an empty string.
See the exceptions section of this msdn article.
Without knowing a bit more about what you are trying to achieve it is difficult to help any further. As it stands if the Replace method were to succeed then the Split method would convert the string to an array and then the ToString method would return "System.String[]". I doubt this is the result you are looking for.
string.Replace
takes replace all insatances of the first argument with the second argument. So "abcabcabc".Replace("a", "z")
becomes "zbczbczbc"
.
Replacing all instances of the empty string is going to lead to an infinate loop of replacements. "".Replace(string.Empty, "a")
replaces the empty string with "a", but there's still an empty string before and after the a, which needs to get replaced and so we have "aaa", but there's still an empty string at the begining and end of the string, as well as between the a's, so replacing those we get "aaaaaaa" etc.
Do you actually mean to use the default date if the value is empty? In which case you want to do something like:
var temp = collection["ED"];
if (string.IsNullOrEmpty(temp)) {
temp = "1/1/2011";
}
string ED = temp.Split(',').ToString();
精彩评论