【问题标题】:What is the shortest way of repeating a character n times in the middle of a string in C#在C#中的字符串中间重复一个字符n次的最短方法是什么
【发布时间】:2018-09-26 08:15:23
【问题描述】:

我目前正在创建一个 ASCII 表构建器,这是 GXP 环境中某些自动化数据库报告所必需的。

鉴于我有宽度为 n 的表格行,例如:

| this | is | an | example | row |
|<--       width = 32         -->|

我现在想添加标题和分隔符,例如:

#================================#
| this | is | an | example | row |
|--------------------------------|
| 1    | 2  | 3  | 4       | 5   |
| 3    | 9  | 77 | 327814  | 2   |
|--------------------------------|

当然,我可以通过以下方式做到这一点:

List<string> asciiTable = new List<string();
string topBorder = "#";
string otherBorder = "|";
for (int i = 1; i == n; i++)
{
    topBorder += "=";
    otherBorder += "-";
}
topBorder += "#";
otherBorder += "|";
asciiTable.Add(topBorder);

但我希望有这样的东西:

List<string> asciiTable = new List<string>();
asciiTable.Add("#" + /* add("=",n) */ + "#"); 

【问题讨论】:

标签: c# string ascii


【解决方案1】:

您可以使用 new String('=', n); 创建一个 string,其中字符“=”重复 n 次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多