TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Uppercase First Letter

Implement a Function to uppercase the first letter in Strings.
Uppercase 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.StringsFunction

Here: We introduce a custom method that uppercases the first letter. A built-in method, ToTitleCase, is also available in the Framework.

Example. First, this program introduces the UppercaseFirstLetter function, which receives a String and returns a String. In this function, we first test for null or empty parameters, and exit early in this case.

Next: We call ToCharArray to convert the String into a mutable array of characters.

Then: We use the Char.ToUpper method to uppercase the first letter, and we finally return a new String instance built from the character array.

Char Array
VB.NET program that uppercases first letter Module Module1 Sub Main() Console.WriteLine(UppercaseFirstLetter("sam")) Console.WriteLine(UppercaseFirstLetter("Codex")) 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.NET
This 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
ToTitleCase. Instead of this custom implementation, you could use the ToTitleCase function from the .NET Framework. With ToTitleCase, every word is capitalized, which is a slightly different behavior from the implementation above.ToTitleCase
Summary. We learned one way you can uppercase only part of a string, such as the first letter. You could develop more sophisticated logic to uppercase or change other parts of the character array. The approach would be equally effective.

Note: This method could be used to clean up poorly formatted character data, as from a text file or database.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf