Skip to content

Instantly share code, notes, and snippets.

@examinedliving
Created November 22, 2019 16:48
Show Gist options
  • Save examinedliving/df01be2b18ae622077245cb6fb587dde to your computer and use it in GitHub Desktop.
Save examinedliving/df01be2b18ae622077245cb6fb587dde to your computer and use it in GitHub Desktop.
Visual Basic Function to capitalize the first letter of the first word in a string that may or may not include multiple words.
Private Function Capitalize(str As String) As String
Dim newStr As String = String.Empty
If str IsNot Nothing Then
Dim l As New List(Of String)(str.Split(" "c))
Dim st As String = l(0)
Dim f As String = st.Substring(0, 1)
Dim rest As String = st.Substring(1)
newStr = String.Join(" ", l).Replace(st, "")
newStr = $"{f.ToUpper() & rest} {newStr}"
newStr = newStr.Replace(" ", " ")
newStr = newStr.Trim()
End If
Return newStr
End Function
@examinedliving
Copy link
Author

Note: The $"{} syntax is interpolation and is only available in Visual Studio 2017 and later. If need be, this can be replaced with String Concatenation in one of the ways Visual Basic offers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment