gpt4 book ai didi

c# - String.Format 未使用 ToShortDateString 和 String 正确对齐

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:41 25 4
gpt4 key购买 nike

这条线

TitleDate = string.Format("Date: {0, -10}  Title: {1, 10}" ,c.Date.ToShortDateString() ,c.Title)

正在显示:

Date: 8/26/2007 Title: A Title
Date: 10/13/2011 Title: A Title
Date: 11/11/2012 Title: A Title
Date: 3/1/2001 Title: A Title

我正在努力

Date: 8/26/2007  Title: A Title
Date: 10/13/2011 Title: A Title
Date: 11/11/2012 Title: A Title
Date: 3/1/2001 Title: A Title

这些值显示在 TreeView 的文本字段中

最佳答案

尝试使用PadRight方法

存在三种可能性,您的日期字符串可能有 8、9 或 10 个字符。所以这是一个可能的(我猜)但不是优雅的解决方案:

string date = c.Date.ToShortDateString();
int lenght = date.Length;
int whiteSpace = 0;
switch (length)
{
case 8:
whitespace = 6;
break;
case 9:
whitespace = 5;
break;
case 10:
whitespace = 4;
break;
}

TitleDate = string.Format("Date: {0} Title: {1}" ,date.PadRight(whiteSpace,' '), c.Title);
TitleDate = TitleDate.Replace(" ","&nbsp");

或者您可以使用自定义 DateTime Format让它更优雅:

TitleDate = string.Format("Date: {0}  Title: {1}" ,c.Date.ToString("dd/MM/yyyy") ,c.Title)

关于c# - String.Format 未使用 ToShortDateString 和 String 正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20864634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com