Question Convert to uppercase

audio_uphoria

Active member
Joined
Jan 28, 2009
Messages
31
Programming Experience
Beginner
Hi, can anyone tell me how to take a name entered in a text box and convert only the first letter of each name to upper and then convert the rest to lower.

Ex: "john doe" --> "John Doe"

"jAkE sMiTh" --> "Jake Smith"

Thanks.
 
Hi,

Here is a code snippet I wrote to create String with Title case.

VB.NET:
Private Function TitleCase(ByVal sentence As String) As String
        ' Split the string into its component words
        Dim sentenceArray() As String = sentence.Split

        ' For each word in the array get 
        Dim newSentence As String = String.Empty
        For Each word As String In sentenceArray
            ' Get the first letter and capitalise it
            Dim firstLetter As String = word.Substring(0, 1).ToUpper()
            ' Remove the first the first letter
            word = word.Remove(0, 1)
            ' Insert the capitalised letter
            word = word.Insert(0, firstLetter)
            ' Append the word into a new sentence
            newSentence &= " " & word
        Next

        ' Return the capitalised string
        Return newSentence

    End Function
Hope this helps

Regards

ScottyB
 
Thanks for the response. I have messed around with converting the first letter to upper then replacing the old letter with the new uppercase one but then I realized the book wants me to be able to convert things like "jAkE" so just changing the first letter wouldnt work in that case.
 
Have a go with this PascalCase method, and read up on regular expressions.
VB.NET:
Function PascalCase(ByVal input As String) As String
    Return Regex.Replace(input.ToLower, "(^| )\w", AddressOf UpperReplace)
End Function

Function UpperReplace(ByVal m As Match) As String
    Return m.Value.ToUpper
End Function
 
Never ceases to amaze me. You learn something new everyday. Trust me to re-invent the wheel.

Very useful will add Regex Functions to my memory bank.

Regards

ScottyB
 
Back
Top