消息框显示时不带空格

本文关键字:空格 显示 消息 | 更新日期: 2023-09-27 18:31:20

当我单击按钮时,我希望弹出一个消息框,其中包含薪水最低的员工。这些员工通过文本框输入并存储在数组中。该类称为"员工"。

我已经让它工作了,但例如,如果我输入 ID:1234,名字:鲍勃,姓氏:样式,薪水:24,消息框将显示:"1234BobStyles24",没有空格。我该如何解决这个问题?这是我的代码:

    private void lowestSalaryButton_Click(object sender, EventArgs e)
    {
        decimal minSalary = employees.Min(em => em.YearSalary);
        IEnumerable<Employee> poors = employees.Where(em => em.YearSalary == minSalary);
        poors.Count();
        foreach (var poor in poors)
        MessageBox.Show(minSalary.ToString("C") + poor.FirstName + poor.LastName +   
        poor.EmployeeId, "Min salary");
                }
            }
        }

消息框显示时不带空格

只需将其更改为:

MessageBox.Show(minSalary.ToString("C") + " " + poor.FirstName  + " " + poor.LastName  + " " + poor.EmployeeId, "Min salary");

或者更好的方法是:

string message = string.Format("{0} {1} {2} {3}", minSalary, poor.FirstName, poor.LastName, poor.EmployeeId);
MessageBox.Show(message, "Min salary");

像这样说:

      string spaceInBetween  =new string(' ', 2);
      MessageBox.Show(minSalary.ToString("C") + 
                  spaceInBetween   +  poor.FirstName + 
                  spaceInBetween   + poor.LastName +  
                  spaceInBetween + poor.EmployeeId, "Min salary");

在你的代码中:

 MessageBox.Show(minSalary.ToString("C") + poor.FirstName + poor.LastName +   
        poor.EmployeeId, "Min salary");

你正是这样做的

您可以使用

String.Format(" {0} {1} [2} {3}, Min salary",minSalary.ToString("C"), poor.FirstName , poor.LastName ,  poor.EmployeeId )

或者,您可以使用

poor.Firstname + " " + poor.Lastname