0
我有一些代碼可以完成我想要的功能,但是我想知道是否有更快的方法來實現我想要的功能。C#大寫首字母的字符串 - 更有效的方法?
用戶將輸入他們的名字,我想,以確保他們的名字是接近這個格式可能:
- 約翰>約翰
- 朱莉>朱莉
我代碼:
// First Name
s_in_GetUserFirstName = s_in_GetUserFirstName.ToLower();
c_in_UserFirstNameFirstChar = s_in_GetUserFirstName[0];
s_in_UserFirstNameFirstChar = c_in_UserFirstNameFirstChar.ToString().ToUpper();
s_in_GetUserFirstName = s_in_GetUserFirstName.Remove(0, 1);
s_in_GetUserFirstName = s_in_UserFirstNameFirstChar + s_in_GetUserFirstName;
// Last Name
s_in_GetUserLastName = s_in_GetUserLastName.ToLower();
c_in_UserLastNameFirstChar = s_in_GetUserLastName[0];
s_in_UserLastNameFirstChar = c_in_UserLastNameFirstChar.ToString().ToUpper();
s_in_GetUserLastName = s_in_GetUserLastName.Remove(0, 1);
s_in_GetUserLastName = s_in_UserLastNameFirstChar + s_in_GetUserLastName;
我所做的是將字符串分成兩部分,第一個字符和休息。我大寫第一個字符並從字符串中刪除該字符。然後我將字符串合併成一個。
建議看看http://stackoverflow.com/a/1206029/563532 - 此格式通常稱爲'標題案例'或'正確案例' – Rob
這是完美的,謝謝! – benfleuty
LINQ:這將工作太:) 變種STR = 小號 .ToCharArray() 。選擇((C,I)=>我== 0 Char.ToUpper(C):Char.ToLower(c)中) 。選擇(c => c.ToString()) .Aggregate((c,n)=> c + n) ; – ipavlu