Created
November 22, 2019 16:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.