Thursday, 7 July 2011

C# method to turn first letter to upper case

Can be done in different ways.


private string TurnFirstLetterToUpper(string s)
{
string firstLetter = s.Substring(0,1);
return firstLetter.ToUpper() + s.Remove(0,1);
}



Response.Write(TurnFirstLetterToUpper("programming")); //Ouput: Programming


Another one


string s = "coding";
string firstLetter = s.Substring(0, 1);
s= s.Replace(firstLetter, firstLetter.ToUpper()); //You should be careful while doing this, bcs String.Replace() will replace all the occurances of "firstLetter" in "s"

Response.Write(s); //Ouput: Coding


String.Replace(string,string):
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

String.Remove(int,int):
Deletes a specified number of characters from the string beginning at a specified position.

No comments:

Post a Comment