ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

UcWords

Returns Title Cae. Capitalise first letter of each word

Source

Public Module StringExtensions
    ''' <summary>
    ''' Capitalise the first letter of each word / Tilte Case
    ''' </summary>
    ''' <param name="words">A string - paragraph or sentence</param>
    ''' <returns>String</returns>
    <Runtime.CompilerServices.Extension()> _
    Public Function UcWords(ByVal words As String)
        Dim output As StringBuilder = New StringBuilder()
        Dim exploded = words.Split({" "c})
        If (exploded IsNot Nothing) Then
            For Each word As String In exploded
                output.Append(word.Substring(0, 1).ToUpper) _
                      .Append(word.Substring(1, word.Length - 1)) _
                      .Append(" ")
            Next
        End If

        Return output.ToString()

    End Function

End Module

Example

Dim sentence As String =  "I am a sentence of words"
Dim capitalised As String = sentence.UcWords()

' capitalised:

"I Am A Sentence Of Words"

Author: Stuart Sillitoe

Submitted on: 7 jul. 2016

Language: VB

Type: String

Views: 4272