使用更新的整数值格式化字符串以与文本框一起显示为列
Format string with updated integer values to display with textbox as columns
如果我的字符串更新后的整数值最多为 8 位:
string Str = ("A1: [ " + int1 + " ] A2: [ " + int2 + " ]" +
"\r\nA3: [ " + int3 + " ] A4: [ " + int4 + " ]" +
"\r\nB1: [ " + int5 + " ] B2: [ " + int6 + " ]" +
"\r\nB3: [ " + int7 + " ] B4: [ " + int2 + " ]\r\n");
即:
A1: [ 126.453 ] A2: [ 8.940 ]
A3: [ 11.3 ] A4: [ 122.4 ]
B1: [ 6566.2 ] B2: [ 3444.76 ]
B3: [ 6.562 ] B4: [ 34.76 ]
如何将其格式化为迭代显示,文本框输出并排显示为列,各部分之间的间距与计数整数大小:
A1: [ 126.453 ] A2: [ 348656.2 ] B1: [ 35.7 ] B2: [ 65.1 ]
A3: [ 8.90 ] A4: [ 34.76 ] B3: [ 3.97770 ] B4: [ 87700.67 ]
我找到了几种方法,但不太确定如何在这种情况下使用它,如何计算出更新后的数字并将我的字符串分配给整数长度:
int pd = 3;
int mx1 = "Hello World!".Length;
int mx2 = "1234.56732".Length;
int mx3 = "Name".Length;
string f0 = "{0,-" + (mx1 + pd) + "}";
string f1 = "{1,-" + (mx2 + pd) + "}";
string f2 = "{2,-" + (mx3 + pd) + "}";
string f = f0 + f1 + f2;
Console.WriteLine(f, "Hello World!", 1234.56732, "Month");
Console.WriteLine(f, "How are you?", 78.44, "Day");
Console.WriteLine(f, "Hi", 2311.654, "Year");
很高兴听到一些有用的建议
您可以使用 String.PadRight()
方法对齐值:
var nums = new List<double>() {
126.453, 8.94, 11.3, 122.4, 6566.2, 3444.76, 6.562, 34.76
};
var maxLen = nums.Select(n => n.ToString().Length).Max();
var strVals = nums.Select(n => n.ToString().PadRight(maxLen)).ToList();
textBox1.Font = new Font(FontFamily.GenericMonospace, 10);
var pos = 0;
foreach (var strVal in strVals)
//Debug.Write($"NUM{++pos}: [ {strVal} ] {(pos % 2 == 0 ? "\r\n" : "")}");
textBox1.Text += $"NUM{++pos}: [ {strVal} ] {(pos % 2 == 0 ? "\r\n" : "")}";
输出:
NUM1: [ 126,453 ] NUM2: [ 8,94 ]
NUM3: [ 11,3 ] NUM4: [ 122,4 ]
NUM5: [ 6566,2 ] NUM6: [ 3444,76 ]
NUM7: [ 6,562 ] NUM8: [ 34,76 ]
如果我的字符串更新后的整数值最多为 8 位:
string Str = ("A1: [ " + int1 + " ] A2: [ " + int2 + " ]" +
"\r\nA3: [ " + int3 + " ] A4: [ " + int4 + " ]" +
"\r\nB1: [ " + int5 + " ] B2: [ " + int6 + " ]" +
"\r\nB3: [ " + int7 + " ] B4: [ " + int2 + " ]\r\n");
即:
A1: [ 126.453 ] A2: [ 8.940 ]
A3: [ 11.3 ] A4: [ 122.4 ]
B1: [ 6566.2 ] B2: [ 3444.76 ]
B3: [ 6.562 ] B4: [ 34.76 ]
如何将其格式化为迭代显示,文本框输出并排显示为列,各部分之间的间距与计数整数大小:
A1: [ 126.453 ] A2: [ 348656.2 ] B1: [ 35.7 ] B2: [ 65.1 ]
A3: [ 8.90 ] A4: [ 34.76 ] B3: [ 3.97770 ] B4: [ 87700.67 ]
我找到了几种方法,但不太确定如何在这种情况下使用它,如何计算出更新后的数字并将我的字符串分配给整数长度:
int pd = 3;
int mx1 = "Hello World!".Length;
int mx2 = "1234.56732".Length;
int mx3 = "Name".Length;
string f0 = "{0,-" + (mx1 + pd) + "}";
string f1 = "{1,-" + (mx2 + pd) + "}";
string f2 = "{2,-" + (mx3 + pd) + "}";
string f = f0 + f1 + f2;
Console.WriteLine(f, "Hello World!", 1234.56732, "Month");
Console.WriteLine(f, "How are you?", 78.44, "Day");
Console.WriteLine(f, "Hi", 2311.654, "Year");
很高兴听到一些有用的建议
您可以使用 String.PadRight()
方法对齐值:
var nums = new List<double>() {
126.453, 8.94, 11.3, 122.4, 6566.2, 3444.76, 6.562, 34.76
};
var maxLen = nums.Select(n => n.ToString().Length).Max();
var strVals = nums.Select(n => n.ToString().PadRight(maxLen)).ToList();
textBox1.Font = new Font(FontFamily.GenericMonospace, 10);
var pos = 0;
foreach (var strVal in strVals)
//Debug.Write($"NUM{++pos}: [ {strVal} ] {(pos % 2 == 0 ? "\r\n" : "")}");
textBox1.Text += $"NUM{++pos}: [ {strVal} ] {(pos % 2 == 0 ? "\r\n" : "")}";
输出:
NUM1: [ 126,453 ] NUM2: [ 8,94 ]
NUM3: [ 11,3 ] NUM4: [ 122,4 ]
NUM5: [ 6566,2 ] NUM6: [ 3444,76 ]
NUM7: [ 6,562 ] NUM8: [ 34,76 ]