How to replace string.empty to “0”

后端 未结 10 1773
无人共我
无人共我 2021-01-27 11:15

Just like the title says.

I\'ve tried doing str.Replace(\"\",\"0\"); but it gave me error because oldValue has zero length.

Is it possi

10条回答
  •  北恋
    北恋 (楼主)
    2021-01-27 11:58

    String.Replace takes two string arguments oldValue and newValue. You specified the newValue 0 however an empty string is not legal for the oldValue.

    try below code :-

    str.Replace(" ","0"); 
    

    or you can just assign "0" to emptry string as below :-

    if(str == string.Empty)
    {
       str = "0";
    }
    

    or making it simple :-

    String.IsNullOrWhiteSpace(str) ? "0" : str;
    

提交回复
热议问题