How to replace string.empty to “0”

后端 未结 10 1783
无人共我
无人共我 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 12:06

    You can't replace empty string within the string, but you can replace, say, spaces, e.g.

      str = str.Replace(" ", "0"); // providing str is not null
    

    Or you can substitute empty string with "0":

      if (String.IsNullOrEmpty(str))
        str = "0";
    

    When parsing string into int you can do something like that:

      int x = String.IsNullOrEmpty(str) ? 0 : Convert.ToInt32(str);
    

提交回复
热议问题