将字符串格式化为列

发布于 2024-09-03 22:09:09 字数 1142 浏览 7 评论 0原文

有没有一种很酷的方法来获取这样的内容:

Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653

并将其格式化为这样的内容:

Customer Name     - City,       State - ID
Bob Whiley        - Howesville, TN    - 322
Marley Winchester - Old Towne,  CA    - 5653

使用字符串格式命令?

如果一篇文章很长,我不太纠结该怎么办。例如,这对我来说没问题:

Customer Name     - City,       State - ID
Bob Whiley        - Howesville, TN    - 322
Marley Winchester - Old Towne,  CA    - 5653
Super Town person - Long Town Name, WA- 45648 

提供一些背景信息。我有一个下拉框,显示与此非常相似的信息。现在,我在下拉列表中创建项目的代码如下所示:

public partial class CustomerDataContract
{
    public string DropDownDisplay
    {
        get
        {
             return  Name + " - " + City + ",  " + State + " - " + ID;
        }
    }
}

我正在寻找一种更好地格式化该项目的方法。有什么想法吗?


这就是我最终得到的结果:

HttpContext.Current.Server.HtmlDecode(
    String.Format("{0,-27} - {1,-15}, {2, 2} - {3,5}", 
    Name, City, State, ID)
    .Replace(" ", " "));

HtmlDecode 更改了  到可以承受删除下拉列表格式的空间的空间。

Is there a cool way to take something like this:

Customer Name - City, State - ID
Bob Whiley - Howesville, TN - 322
Marley Winchester - Old Towne, CA - 5653

and format it to something like this:

Customer Name     - City,       State - ID
Bob Whiley        - Howesville, TN    - 322
Marley Winchester - Old Towne,  CA    - 5653

Using string format commands?

I am not too hung up on what to do if one is very long. For example this would be ok by me:

Customer Name     - City,       State - ID
Bob Whiley        - Howesville, TN    - 322
Marley Winchester - Old Towne,  CA    - 5653
Super Town person - Long Town Name, WA- 45648 

To provide some context. I have a drop down box that shows info very similar to this. Right now my code to create the item in the drop down looks like this:

public partial class CustomerDataContract
{
    public string DropDownDisplay
    {
        get
        {
             return  Name + " - " + City + ",  " + State + " - " + ID;
        }
    }
}

I am looking for a way to format this better. Any ideas?


This is what I ended up with:

HttpContext.Current.Server.HtmlDecode(
    String.Format("{0,-27} - {1,-15}, {2, 2} - {3,5}", 
    Name, City, State, ID)
    .Replace(" ", " "));

The HtmlDecode changes the   to a space that can withstand the space removing formatting of the dropdown list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来世叙缘 2024-09-10 22:09:09

您可以使用Console.WriteLine或使用String.Format指定文本占用的列数以及对齐方式:

// Prints "--123       --"
Console.WriteLine("--{0,-10}--", 123);
// Prints "--       123--"
Console.WriteLine("--{0,10}--", 123);

数字指定要使用的列数符号指定对齐方式(- 表示左对齐,+ 表示右对齐)。因此,如果您知道可用的列数,您可以编写如下内容:

public string DropDownDisplay { 
  get { 
    return String.Format("{0,-10} - {1,-10}, {2, 10} - {3,5}"),
      Name, City, State, ID);
  } 
} 

如果您想根据整个列表(例如最长的名称)计算列数,那么您需要获取提前该数字并将其作为参数传递给您的 DropDownDisplay - 无法自动执行此操作。

You can specify the number of columns occupied by the text as well as alignment using Console.WriteLine or using String.Format:

// Prints "--123       --"
Console.WriteLine("--{0,-10}--", 123);
// Prints "--       123--"
Console.WriteLine("--{0,10}--", 123);

The number specifies the number of columns you want to use and the sign specifies alignment (- for left alignment, + for right alignment). So, if you know the number of columns available, you could write for example something like this:

public string DropDownDisplay { 
  get { 
    return String.Format("{0,-10} - {1,-10}, {2, 10} - {3,5}"),
      Name, City, State, ID);
  } 
} 

If you'd like to calculate the number of columns based on the entire list (e.g. the longest name), then you'll need to get that number in advance and pass it as a parameter to your DropDownDisplay - there is no way to do this automatically.

口干舌燥 2024-09-10 22:09:09

除了 Tomas 的回答之外,我只想指出字符串插值可以在 C# 6 或更高版本中使用。

// with string format
var columnHeaders1 = string.Format($"|{0,-30}|{1,-4}|{2,-15}|{3,-30}|{4,-30}|{5,-30}|{6,-30}", "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG");

// with string interpolation
var columnHeaders2 = $"|{"ColumnA",-30}|{"ColumnB",-4}|{"ColumnC",-15}|{"ColumnD",-30}|{"ColumnE",-30}|{"ColumnF",-30}|{"ColumnG",-30}";

In addition to Tomas's answer I just want to point out that string interpolation can be used in C# 6 or newer.

// with string format
var columnHeaders1 = string.Format($"|{0,-30}|{1,-4}|{2,-15}|{3,-30}|{4,-30}|{5,-30}|{6,-30}", "ColumnA", "ColumnB", "ColumnC", "ColumnD", "ColumnE", "ColumnF", "ColumnG");

// with string interpolation
var columnHeaders2 = $"|{"ColumnA",-30}|{"ColumnB",-4}|{"ColumnC",-15}|{"ColumnD",-30}|{"ColumnE",-30}|{"ColumnF",-30}|{"ColumnG",-30}";
世界如花海般美丽 2024-09-10 22:09:09

我无法在上面添加评论,但在接受的答案中指出:

如果您想根据整个列表(例如最长的名称)计算列数,那么您需要提前获取该数字并将其作为参数传递给 DropDownDisplay - 无法自动执行此操作

事实上,这可以通过“动态”创建格式字符串在运行时以编程方式完成:

string p0 = "first";
string p1 = "separated by alignment value x";

int x = n * 10; // calculate the alignment x as needed

// now use x to give something like: {0,-20}, {1}
string fmt = "{0,-" + x + "},{1}"; // or whatever formatting expression you want

// then use the fmt string
string str = string.Format(fmt, p0, p1)

// with n = 2 this would give us
"first               ,separated by alignment value x"

I am unable to add a comment above, but in the accepted answer it was stated:

If you'd like to calculate the number of columns based on the entire list (e.g. the longest name), then you'll need to get that number in advance and pass it as a parameter to your DropDownDisplay - there is no way to do this automatically.

This can in fact be done programmatically at runtime by creating the format string 'on the fly':

string p0 = "first";
string p1 = "separated by alignment value x";

int x = n * 10; // calculate the alignment x as needed

// now use x to give something like: {0,-20}, {1}
string fmt = "{0,-" + x + "},{1}"; // or whatever formatting expression you want

// then use the fmt string
string str = string.Format(fmt, p0, p1)

// with n = 2 this would give us
"first               ,separated by alignment value x"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文