简体   繁体   English

如何将string.empty替换为“ 0”

[英]How to replace string.empty to “0”

Just like the title says. 就像标题说的那样。

I've tried doing str.Replace("","0"); 我试着做str.Replace("","0"); but it gave me error because oldValue has zero length. 但这给了我错误,因为oldValue长度为零。

Is it possible to replace string.Empty into something? 是否可以将string.Empty替换为某些内容?

Edit: 编辑:

I am maintaining a program and I encountered that the program was calling a method that'll return a string then be converted to Int32. 我正在维护一个程序,但遇到程序正在调用将返回字符串然后转换为Int32的方法。

int x = Convert.toInt32(Method1()); int x = Convert.toInt32(Method1());

public string Method1()
{
      string retString = string.Empty;
      //Do Something
      return retString
}

String.Replace takes two string arguments oldValue and newValue. String.Replace具有两个字符串参数oldValue和newValue。 You specified the newValue 0 however an empty string is not legal for the oldValue. 您将newValue指定为0,但是对于oldValue而言,空字符串不合法。

try below code :- 尝试下面的代码:-

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

or you can just assign "0" to emptry string as below :- 或者您也可以将空字符串指定为“ 0”,如下所示:

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

or making it simple :- 或简单点:

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

You can't replace empty string within the string, but you can replace, say, spaces , eg 您不能替换 字符串中的空字符串 ,但是可以替换,例如, 空格

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

Or you can substitute empty string with "0" : 或者,您可以空字符串替换"0"

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

When parsing string into int you can do something like that: string 解析int您可以执行以下操作:

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

在method()中,您可以执行以下操作:

return String.IsNullOrEmpty(retString) ? "0" : retString;

您可以使用此单行代码简单地为空,零长度或空格字符串返回"0"

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

如果要检查该值是否为空,然后将该值设置为零,否则使用默认值,可以使用内联,如下所示:

return string.IsNullOrWhiteSpace(retString ) ? "0" : retString;

如果知道str为空,则可以使用star =“ a”。如果要检查,请在if条件中编写语句。

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. 要将空字符串转换为0并将非空字符串解析为整数,我根本不会处理"0" ,而是将两者合并在一个方法中。 For example: 例如:

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

  return int.Parse(s);
}

Try This... 尝试这个...

public string Method1()
{
      string retString = string.Empty;
      //Do Something
      return string.IsNullOrEmpty(retString)?"0":retString;
}

It is not possible to replace string.Empty by "0". 无法将string.Empty替换为“ 0”。 It will throw ArgumentException. 它将引发ArgumentException。

    An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll.
Additional information: String cannot be of zero length.

You can try following code: 您可以尝试以下代码:

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

It sounds like your best option would be int.TryParse , if you encounter a string that can't be set to a valid value, it will set it to the default value for an integer (0) as well as returning a boolean so you can know this has happened. 听起来,最好的选择是int.TryParse ,如果遇到无法设置为有效值的字符串,它将把它设置为整数(0)的默认值,并返回一个布尔值,因此您可以知道这已经发生了。

int myInt;
if(!int.TryParse(myStringVariable, out myInt))
{
    //Invalid integer you can put something here if you like but not needed
    //myInt has been set to zero
}
//The same code without the if statement, still sets myInt
int.TryParse(myStringVariable, out myInt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM