.NET Array Dictionary List String Sub ArrayList Cast Class Console Dates DataTable DateTime Enum File For Format If IndexOf Lambda LINQ Nothing Parse Process Property Regex Replace Select Sort Split StringBuilder SubstringUppercase First. This Function uppercases the first letter. It acts upon a VB.NET String. The value "test" becomes "Test". None of the other characters should be modified. And if the first character is whitespace or a digit, it should not be affected.
Here: We introduce a custom method that uppercases the first letter. A built-in method, ToTitleCase, is also available in the Framework.
VB.NET program that uppercases first letter Module Module1 Sub Main() Console.WriteLine(UppercaseFirstLetter("sam")) Console.WriteLine(UppercaseFirstLetter("perls")) Console.WriteLine(UppercaseFirstLetter(Nothing)) Console.WriteLine(UppercaseFirstLetter("VB.NET")) End Sub Function UppercaseFirstLetter(ByVal val As String) As String ' Test for nothing or empty. If String.IsNullOrEmpty(val) Then Return val End If ' Convert to character array. Dim array() As Char = val.ToCharArray ' Uppercase first character. array(0) = Char.ToUpper(array(0)) ' Return new string. Return New String(array) End Function End Module Output Sam Perls VB.NETThis function, which uses the ToCharArray constructor, is somewhat faster and less memory-intensive than a version that uses the Substring method multiple times. It cuts down on the number of new string instances allocated.
Tip: In performance analysis, eliminating string allocations is often a clear win. Using ToCharArray here helps.
Warning: This function has a limitation. It won't correctly process names that have multiple uppercase letters in them.
So: The name "McChrystal" would be changed to "Mcchrystal" if the input string was "mcchrystal". This could be corrected with a Dictionary.Dictionary
Note: This method could be used to clean up poorly formatted character data, as from a text file or database.