Visual Studio 和 string.format 在新行参数中出现问题
本文关键字:参数 问题 新行 Studio string format Visual | 更新日期: 2023-09-27 18:30:43
我覆盖了我的ToString()方法,我的类包含许多字段。
我只能在最后一个字符串之后有新行,并且只将变量放在下一行。
注意 - 这仅用于视觉目的,我不希望在打印之间分开一行。只是为了在我的 21 英寸屏幕上看到完整的线条而不滚动。
这有效:
public override string ToString()
{
return String.Format("{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4}, Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}",
base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
}
但这不起作用(相同的代码,仅在第 4 个参数后按回车键):
public override string ToString()
{
return String.Format("{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4},
Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}",
base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
}
String
是引号之间的一行中的符号序列。
"one two" - is string.
"one
two" - is an error.
可以在 C# 中通过串联有效字符串来创建多行字符串:
String s = "one" + Environment.NewLine +
" two";
或在字符串前添加@
String s = @"one
two";
使用逐字字符串,以 @ 字符开头的字符串:
public override string ToString()
{
return String.Format(@"{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4},
Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}",
base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
}
我唯一能想到的如下:
public override string ToString()
{
return String.Format(
"{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3},"
+ " Model = {4}, Firmware = {5}, Driver = {6}, MacAddress = {7},"
+ " Scenes count = {8}",
base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
}
试试这个:
string temp_var = @"{0}, IsAccessible = {1}, NetworkAddress = {2}, Manufacturer = {3}, Model = {4},
Firmware = {5}, Driver = {6}, MacAddress = {7}, Scenes count = {8}";
temp_var = string.Format(base.ToString(), IsAccessible, NetworkAddress, Manufacturer, Model, Firmware, Driver, MacAddress, Scenes.Count);
return temp_var;
只需将值分配给字符串变量即可。