gpt4 book ai didi

.NET 用固定空格格式化字符串

转载 作者:行者123 更新时间:2023-12-01 22:57:19 25 4
gpt4 key购买 nike

.NET String.Format 方法是否允许将字符串放置在固定长度字符串内的固定位置。

“字符串在这里”
“字符串在这里”
“字符串在这里”

这是如何使用 .NET 完成的?

编辑 - 我已经尝试过 Format/PadLeft/PadRight 死了。它们不起作用。我不知道为什么。我最终编写了自己的函数来做到这一点。

编辑 - 我犯了一个错误,在格式说明符中使用了冒号而不是逗号。应该是“{0,20}”。

感谢所有优秀和正确的答案。

最佳答案

这将为您提供您要求的确切字符串:

string s = "String goes here";
string lineAlignedRight = String.Format("{0,27}", s);
string lineAlignedCenter = String.Format("{0,-27}",
String.Format("{0," + ((27 + s.Length) / 2).ToString() + "}", s));
string lineAlignedLeft = String.Format("{0,-27}", s);

关于.NET 用固定空格格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/644017/

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