我是 C# 新手。我正在大学的一个模块中学习它。我们接到了一项任务,要求我们使用 Visual Studio 工具箱中包含的各种组件创建一个简单的预订应用程序。
UI 有一个 ListBox
,它使用户能够选择将参加事件的多个人的名字。当用户确认选择时,所选项目连接到 String
并在 Label
中输出。
这是我从 ListBox
protected void btnRequest_Click(object sender, EventArgs e)
{
//Update the summary label with the details of the booking.
n = name.Text;
en = eventName.Text;
r = room.SelectedItem.ToString();
d = cal.SelectedDate.ToShortDateString();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
people += li.Text + " ";
}
}
confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + people + " will be attending.";
}
下面是我的全部代码:
public partial class _Default : System.Web.UI.Page
{
//Variables
public TextBox name;
public TextBox eventName;
public Label confirmation;
public DropDownList room;
public Calendar cal;
public Button btn;
public ListBox attendees;
//Booking variables - store all information relating to booking in these variables
public String n; //name of person placing booking
public String en; //name of event
public String r; //room it will take place
public List<String> att; //list of people attending
public String d; //date it will be held on
public String people;
protected void Page_Load(object sender, EventArgs e)
{
//Get references to components
name = txtName;
eventName = txtEvent;
room = droplistRooms;
attendees = attendeelist;
cal = Calendar1;
btn = btnRequest;
confirmation = lblSummary;
}
protected void btnRequest_Click(object sender, EventArgs e)
{
//Update the summary label with the details of the booking.
n = name.Text;
en = eventName.Text;
r = room.SelectedItem.ToString();
d = cal.SelectedDate.ToShortDateString();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
people += li.Text + " ";
}
}
confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + people + " will be attending.";
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
d = cal.SelectedDate.ToShortDateString();
}
输出是这样的:
Jason Manford 已于 2013 年 8 月 10 日为 Comedy Gig 预订了房间 2。 Jack Coldrick Bill Gates Larry Page Jimmy Wales 将出席。
但是我想在参加事件的人的姓氏上加上一个和。我将如何去做这件事。我必须使用 List
吗?
非常感谢...
最佳答案
尝试将所选项目复制到另一个集合并使用一个简单的计数器:
int counter = 1; // start at 1 so that the counter is in line with the number of items that the loop has iterated over (instead of 0 which would be better for indexing into the collection)
List<ListItem> selectedItems = new List<ListItem>();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
selectedItems.Add(li);
}
}
foreach (ListItem li in selectedItems)
{
counter++;
if (selectedItems.Count > 1 && i == selectedItems.Count) // check after the counter has been incremented so that only the last item triggers it
{
people += " and";
}
people += li.Text + " ";
}
正如一些人所指出的,您还应该考虑使用 StringBuilder
,因为字符串在 .Net 中是不可变的,这意味着它们不能被修改。每次将文本附加到字符串时,都会在幕后创建一个包含新内容的新字符串,并丢弃旧字符串。可以想象,如果列表中有很多名字,这最终可能会影响性能。示例如下:
List<ListItem> selectedItems = new List<ListItem>();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
selectedItems.Add(li);
}
}
StringBuilder sbPeople = new StringBuilder();
int counter = 1; // start at 1 so that the counter is in line with the number of items that the loop has iterated over (instead of 0 which would be better for indexing into the collection)
foreach (ListItem li in attendees.SelectedItems)
{
counter++;
if (selectedItems.Count > 1 && i == selectedItems.Count) // check after the counter has been incremented so that only the last item triggers it
{
sbPeople.Append(" and");
}
sbPeople.Append(li.Text);
sbPeople.Append(" ");
}
引用 StringBuilder
文档:http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
关于c# - 格式化字符串的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19139107/