[Resolved] String cannot be of zero length. Parameter name: oldValue

Dung Do Tien Sep 26 2020 555

I have the code below when I replace variable text0 with text1. I got an error String cannot be of zero length. Parameter name: oldValue.

string text0 = string.Empty;
string text1 = "This is text 1";
string text2 = text1.Replace(text0, "");
Console.WriteLine(text2);

I don't know what is oldValue param? and How can I fix this issue?

Have 1 answer(s) found.
  • M

    Marry Christ Sep 26 2020

    In c#/ Asp.net Core, Replace() method accepts two parameters as below:

    public String Replace(String oldValue, String? newValue); 

    In that, oldValue parameter is required and not accept value null or empty. In your example text0 variable is empty so it throws that exception. Please sure that text0 must NOT null or empty.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close