C# String.Format赢得';t创建正确的间距,用尽所有方法
这似乎是一个微不足道的问题,但我发誓我已经用尽了我能找到的所有方法。我正在尝试将字典的内容输出到文本框。这是用C写的。Idk它有多重要,但我正在输出到WPF文本框。我尝试了以下方法:C# String.Format赢得';t创建正确的间距,用尽所有方法,c#,dictionary,spacing,string.format,C#,Dictionary,Spacing,String.format,这似乎是一个微不足道的问题,但我发誓我已经用尽了我能找到的所有方法。我正在尝试将字典的内容输出到文本框。这是用C写的。Idk它有多重要,但我正在输出到WPF文本框。我尝试了以下方法: Dictionary<string, int> nGramDictionary = StatisticalBreakDown.getNGramFrequency(filePath, 3); MasterStatsTextBlock.Text += "~N-Gram Frequency~" + "\r\n
Dictionary<string, int> nGramDictionary = StatisticalBreakDown.getNGramFrequency(filePath, 3);
MasterStatsTextBlock.Text += "~N-Gram Frequency~" + "\r\n";
foreach (var kvp in nGramDictionary)
{
MasterStatsTextBlock.Text += string.Format("{0,-40}{1}{2}", kvp.Key, kvp.Value, Environment.NewLine);
}
MasterStatsTextBlock.Text += "\r\n";
请帮忙。我真的很不明白为什么这些不起作用 我怀疑问题在于您使用的是
文本框,我猜您没有将文本设置为单空格字体。。。但是您尝试使用字符串格式来准确定位值
为了研究字符串格式,我建议改用控制台应用程序。例如,下面的演示显示字符串格式正确工作。键和值都具有最大长度,键左对齐,值右对齐:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var data = new Dictionary<string, int>
{
{ "first", 10 },
{ "second", 1 },
{ "third", 100000 }
};
foreach (var entry in data)
{
Console.WriteLine("{0,-20}{1,8}", entry.Key, entry.Value);
}
}
}
使用系统;
使用System.Collections.Generic;
班级计划
{
静态void Main(字符串[]参数)
{
var数据=新字典
{
{“第一”,10},
{“第二”,1},
{“第三”,100000}
};
foreach(数据中的var条目)
{
WriteLine({0,-20}{1,8}),entry.Key,entry.Value);
}
}
}
在你的WPF用户界面中尝试一下,你可能会看到同样的断断续续——除非你将字体设置为单空格字体
然而,单空格字体可能看起来很难看。。。在这种情况下,您可能根本不想为此使用文本框。您可以使用其他更高级的基于文本的控件,也可以使用更适合显示数据列表/网格的控件。您是否将字体设置为非比例字体?当角色有不同的尺寸时,我会惊讶地看到任何东西都能起作用。我还想问,文本框是否是显示这些数据的最合适方式。出于好奇,键
值中是否有空白字符?如果在写入条目时将kvp.Key
替换为kvp.Key.Trim()
,结果是否有任何不同?使用此string.Format(“{2}”
并检查(或发布)输出。
Dictionary<string, int> nGramDictionary = StatisticalBreakDown.getNGramFrequency(filePath, 3);
MasterStatsTextBlock.Text += "~N-Gram Frequency~" + "\r\n";
foreach (var kvp in nGramDictionary)
{
MasterStatsTextBlock.Text += string.Format("{0}\t\t\t{1}{2}", kvp.Key, kvp.Value, Environment.NewLine);
}
MasterStatsTextBlock.Text += "\r\n";
~N-Gram Frequency~
talan kirk book 1
kirk book of 1
book of mormon 1
of mormon am 1
mormon am tt 1
am tt extraction 1
tt extraction nephi 1
extraction nephi nephi 1
nephi nephi the 1
nephi the lord 1
the lord speaks 1
lord speaks to 1
speaks to his 1
to his children 1
his children the 1
children the savior 1
the savior teaches 1
savior teaches plainly 1
teaches plainly because 1
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
var data = new Dictionary<string, int>
{
{ "first", 10 },
{ "second", 1 },
{ "third", 100000 }
};
foreach (var entry in data)
{
Console.WriteLine("{0,-20}{1,8}", entry.Key, entry.Value);
}
}
}