您可以在冒号前获取文本并在a中使用它switch
.获取冒号后的文本,因此您不必为此重复代码:
string detail = GetDetail(); int index = detail.IndexOf(':'); if (index != -1) { string before = detail.Substring(0, index); string after = detail.Substring(index + 1).Trim(); switch (before) { case "stackoverflow": detailItem.Stackoverflow = after; break; case "google": detailItem.Google = after; break; case "facebook": detailItem.Facebook = after; break; case "twitter": detailItem.Twitter = after; break; } }