Tuesday, June 5, 2012

How to Quickly Convert First Character to Upper Case Without Code

I got the User Name with Windows Authentication enforce in my asp.net application,

since i want to display the username in the right hand corner, and requirement said that

the user name must with first letter to be in upper case.

usually we will use this approach FirstChar.ToUpper() +MyString.SubString(1) to
return the desire format of the user name.

actually in .net framework there is built in function to handle it.

string strAccount = HttpContext.Current.User.Identity.Name;

 if (strAccount.Split("\\".ToCharArray()).Length >= 2)
 {
   strUserName = strAccount.Split("\\".ToCharArray())[1];
 }

CultureInfo.InvariantCulture.TextInfo.ToTitleCase(strUserName);

No comments:

Post a Comment