Tuesday, 3 August 2010

How to Capitalize the First Letter of Every Word in a String

Neat little bit of coding to help anyone capitalize or make upper case the first letter of every word in a string. Quite handy this little script as first it will make sure that all letters in the string are formatted correctly...

So in VB.Net we have :

in the page load

Dim objconvertText As convertText
objconvertText = New convertText
MsgBox(objconvertText.convert("CAPITALIZE THIS!"))

and for the class

Public Function convert(ByVal theWord As String)
Dim capitalWord
capitalWord = LCase(theWord)
capitalWord = StrConv(capitalWord, VbStrConv.ProperCase)
Return capitalWord.ToString
End Function

and the same in C# is in the page load:

MessageBox.Show(convertTextFirst.capitalText("CAPITALIZE THIS!"));

and then in a class :

public static string capitalText(string theWord)
{string capitalWord;
capitalWord= theWord.ToLower();
capitalWord = char.ToUpper(capitalWord[0]) + capitalWord.Substring(1);
return capitalWord.ToString(); }

No comments:

Post a Comment