How to replace string.empty to “0”

后端 未结 10 1774
无人共我
无人共我 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:16

    After your edit:

    To convert an empty string to 0, and parse a non-empty string as an integer, I wouldn't deal with a "0" at all, but combine the two in a single method. For example:

    int Parse(string s, int d = 0) {
      if (string.IsNullOrEmpty(s))
        return d;
    
      return int.Parse(s);
    }
    

提交回复
热议问题