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 String.Compare Examples

Use String.Compare, String.CompareOrdinal and CompareTo to determine the ordering of strings.
String.Compare. Two Strings can be compared. With Compare, CompareOrdinal, and CompareTo, we can determine whether one String is ordered before another String. This is how sorting routines are implemented.Strings

Tip: Compare returns just three values: -1, 0 and 1. This number indicates the relation of the two strings being compared.

Example. Let's get started with this simple example. We declare two String local variables. Next we call the Shared String.Compare function. It returns -1. This means that String a is smaller than, or comes before, String b.Shared

Next: The result of 1 indicates that String "b" is larger than String a. And 0 means the two Strings are equal.

CompareOrdinal: This performs the same logic but treats each character as an ordinal value. This means Chars are treated by their numeric value.

CompareTo: With CompareTo, we use String instances to perform the comparison. The results of CompareTo are the same as the results of String.Compare.

VB.NET program that uses compare functions Module Module1 Sub Main() Dim a As String = "a" Dim b As String = "b" Dim c As Integer = String.Compare(a, b) Console.WriteLine(c) c = String.CompareOrdinal(b, a) Console.WriteLine(c) c = a.CompareTo(b) Console.WriteLine(c) c = b.CompareTo(a) Console.WriteLine(c) c = "x".CompareTo("x") Console.WriteLine(c) End Sub End Module Output -1 1 -1 1 0
Discussion. As noted, Compare and CompareTo are used for sorting algorithms. In a sort, each String must be compared to other Strings. With many .NET Framework methods, an Enum of StringComparison type can be specified as an argument.

And: Internally, those methods will use a Compare method based on the Enum argument.

Enum

So: StringComparison.Ordinal will result in the CompareOrdinal method being used in some way.

LastIndexOf
Summary. We explored the String.Compare, CompareOrdinal, and Compare functions. If the first String is ordered first, we receive the value -1. If the Strings are equal, we receive 0. And we receive 1 if the second String is first.
© 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