我有一个名为listbox1
收集电子邮件的列表框。我希望listbox1
在项目textbox1
选择该项目。
我可以做到的:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
所有电子邮件都具有相同的格式:[email protected]
,[email protected]
。我希望文本框中的所选项目不包含该@gmail.com
部分。
我怎样才能做到这一点?
列表框中收集的项目是:
在这种情况下,我只想服用ab
或bc
或gh
。
您可以使用String.IndexOf方法找到@
符号的位置,然后使用String.Substring从电子邮件中获取用户名:
var emails = new List<String>()
{
"[email protected]",
"[email protected]",
"[email protected]"
};
emails.ForEach(email =>
{
Console.WriteLine(email.Substring(0, email.IndexOf("@")));
});
example-input的输出为:
abc
xyz123
a
应用于文本框的情况下,解决方案如下所示:
var email = listBox1.SelectedItem.ToString();
textBox1.Text = = email.Substring(0, email.IndexOf("@"));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句