簡體   English   中英

字符之間的間隔

[英]Space between character

我正在使用以下代碼在C#打印中打印DateTimePicker選定的日期。

string theDate1 = dateTimePicker1.Value.ToString("ddMMyyyy");
g.DrawString("" + theDate1 + "  .", new Font("Arial", 19, FontStyle.Regular), Brushes.Brown, new PointF(510, 68));

結果是這樣的: 15072015

我需要在這些日期之間保持空格。 如果我編寫此代碼以僅打印每種類型中的日期,月份或年份,則需要在字符之間保留空格。 例如:我需要這樣的結果: 1 5 0 7 2 0 1 5我該怎么做

將其添加到頁面頂部的using指令中:

using System.Linq;

然后只需像這樣使用Aggregate

theDate1 = theDate1.Aggregate(string.Empty, (d, i) => d + i + ' ');//1 5 0 7 2 0 1 5

嘗試這個

StringBuilder SB= new     StringBuilder();

foreach (char ch in theDate1)
{
    SB.Append(ch);
    SB.Append(" ");
}
var result =SB.ToString().TrimEnd();

另一種方法是這樣的:

string theDate1 = dateTimePicker1.Value.ToString("ddMMyyyy");
theDate1 = string.Join(" ", theDate1.AsEnumerable());

最簡單的方法:

for (int i = 0; i < theDate1.Length; i++) {
    Console.Write(theDate1[i].ToString() + " "); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM