String comparison in .NET (2023)

  • article

.NET provides several methods of comparing string values. The table below shows and describes the price comparison methods.

method nameuse
strict comparisonCompares the values ​​of two strings. Returns an integer value.
String.ComparisonOrdinalIt compares two strings without considering the local culture. Returns an integer value.
String.CompareToCompares the current String object to another String. Returns an integer value.
String.StartsWithDetermines whether the string starts with the passed string. Returns a Boolean value.
String.EndsWithDetermines whether the string ends with the passed string. Returns a Boolean value.
string. containsDetermines whether a character or string appears in another string. Returns a Boolean value.
strictly equal toDetermines if two strings are equal. Returns a Boolean value.
string indexReturns the index position of a character or string, starting at the beginning of the string you're looking at. Returns an integer value.
String.LastIndexOfReturns the index position of a character or string, starting from the end of the string you're looking at. Returns an integer value.

Comparemethod

yetstrict comparisonThe method provides a detailed way to compare two strings. This approach is culturally aware. You can use this function to compare two strings or substrings of two strings. Additionally, case and culture sensitive overloads are provided. The following table lists the three integer values ​​that this method can return.

return valueHealth status
negative integerIn sort order, the first string precedes the second.

-the-

The first string isBlank.

0The first and second strings are the same.

-the-

Both strings areBlank.

positive integer

-or-

1

In sort order, the first string comes after the second string.

-the-

The second string isBlank.

great

Thisstrict comparisonThe method is mainly used when sorting or sorting strings. No needstrict comparisonmethod to check for equality (that is, an explicit search returns a value of 0 regardless of whether one string is less than or greater than the other). Use instead to determine if two strings are equalString.Equals(String, String, String compare)method.

The following example usesstrict comparisonmethod of determining the relative value of two chords.

String^ string1 = "Hello World!";Console::WriteLine(String::Compare(string1, "Hello World?"));
string string1 = "Hello World!";Console.WriteLine(String.Compare(string1, "Hello World?"));
Dim string1 As String = "Hello World!"Console.WriteLine(String.Compare(string1, "Hello World?"))

This example shows-1on the console.

The previous examples are culture sensitive by default. To perform a crop-sensitive string comparison, use the overloadstrict comparisonThe method allows you to determine the crop to use by givingcivilizationfield of application. For an example showing how to use itstrict comparisonFor methods that perform culture-sensitive comparisons, seeStrict comparison with a sense of culture.

order of comparisonmethod

ThisString.ComparisonOrdinalmethod compares two String objects regardless of locale. The return value of this method is the same as this oneComparemethods in the table above.

great

ThisString.ComparisonOrdinalThe method is mainly used when sorting or sorting strings. No needString.ComparisonOrdinalmethod to check for equality (that is, an explicit search returns a value of 0 regardless of whether one string is less than or greater than the other). Use instead to determine if two strings are equalString.Equals(String, String, String compare)method.

The following example usesorder of comparisonmethod compares the values ​​of two strings.

String^ string1 = "Hello World!";Κονσόλα::WriteLine(String::CompareOrdinal(string1, "hello world!"));
string string1 = "Hello World!";Console.WriteLine(String.CompareOrdinal(string1, "hello world!"));
Dim string1 As String = "Hello World!"Console.WriteLine(String.CompareOrdinal(string1, "hello world!"))

This example shows-32on the console.

compared tomethod

ThisString.CompareToThe method compares the string encapsulated by the current string object to another string or object. The return value of this method is the same as this onestrict comparisonmethods in the table above.

great

ThisString.CompareToThe method is mainly used when sorting or sorting strings. No needString.CompareTomethod to check for equality (that is, an explicit search returns a value of 0 regardless of whether one string is less than or greater than the other). Use instead to determine if two strings are equalString.Equals(String, String, String compare)method.

The following example usesString.CompareTocomparison methodstrictly 1Be againststrictly 2Purpose.

String^ string1 = "Hello World";String^ string2 = "Hello World!";int MyInt = string1->CompareTo(string2);Κονσόλα::WriteLine( MyInt );
String string1 = "Hello World";String string2 = "Hello World!";int MyInt = string1.CompareTo(string2);Console.WriteLine(MyInt);
Dim string1 As String = "Hello World"Dim string2 As String = "Hello World!"Dim MyInt As Integer = string1.CompareTo(string2)Console.WriteLine(MyInt)

This example shows-1on the console.

total overloadString.CompareTomethod performs a culture- and case-sensitive comparison by default. There is no overloading of this method, so you can perform culture-sensitive comparisons. For code clarity, we recommend usingstrict comparisonmethod, but please specifyCultural information Current culturefor culturally sensitive businesses orCultural information Unchanged cultureUsed for culturally sensitive functions. For an example showing how to use itstrict comparisonFor methods of performing culture-sensitive and culture-insensitive comparisons, seeIt makes a rigorous, culture-sensitive comparison.

justmethod

Thisstrictly equal toThe method can easily determine if two strings are equal. This case-sensitive method returns agenuinetheInaccurateBoolean value. It can be used from existing classes, as shown in the next example. The following example usesjustmethod to determine if a String object contains the phrase "Hello World".

String^ string1 = "Hello World";Κονσόλα::WriteLine(string1->Equals("Hello World"));
string string1 = "Hello World";Console.WriteLine(string1.Equals("Hello World"));
Dim string1 As String = "Hello World"Console.WriteLine(string1.Equals("Hello World"))

This example showsgenuineon the console.

This method can also be used as a static method. The following example compares two String objects using a static method.

String^ string1 = "Hello World";String^ string2 = "Hello World";Κονσόλα::WriteLine(String::Equals(string1, string2));
String string1 = "Hello World"; String string2 = "Hello World";Console.WriteLine(String.Equals(string1, string2));
Dim String 1 As String = "Hello World" Dim String 2 As String = "Hello World" Console.WriteLine(String.Equals(string1, string2))

This example showsgenuineon the console.

with. . Principleandwith. . Exitmethod

you can use itString.StartsWithmethod to determine if a string object starts with the same characters as another string containing. This case-sensitive method returnsgenuineIf the current string object starts with the passed string andInaccurateif it does not exist. The following example uses this method to determine if a String object begins with "Hello".

String^ string1 = "Hello World";Κονσόλα::WriteLine(string1->StartsWith("Hello"));
string string1 = "Hello World";Console.WriteLine(string1.StartsWith("Hello"));
Dim string1 As String = "Hello World!"Console.WriteLine(string1.StartsWith("Hej"))

This example showsgenuineon the console.

ThisString.EndsWithThe method compares the passed string to the characters at the end of the current string object. It also returns a boolean value. The following example checks the end of a string usingwith. . Exitmethod.

String^ string1 = "Hello World"; Κονσόλα::WriteLine(string1->EndsWith("Hello"));
string string1 = "Hello World";Console.WriteLine(string1.EndsWith("Hello"));
Dim string1 As String = "Hello World!"Console.WriteLine(string1.EndsWith("Hej"))

This example showsInaccurateon the console.

indexandlast indexmethod

you can use itstring indexThe method determines the position of the first occurrence of a specified character in a string. This case-sensitive method counts from the beginning of the string and returns the position of the passed character using a zero-based index. If the character is not found, the value –1 is returned.

The following example usesindexSearch method for the first occurrence of 'Icharacters in the string.

String^ string1 = "Hello World"; Console::WriteLine(string1->IndexOf('l'));
string string1 = "Hello World";Console.WriteLine(string1.IndexOf('l'));
Dim string1 As String = "Hello World!"Console.WriteLine(string1.IndexOf("l"))

This example shows2on the console.

ThisString.LastIndexOfmethod corresponding tostring indexmethod, except that it returns the position of the last occurrence of a given character in a string. It is case sensitive and uses zero indexing.

The following example useslast indexsearch method for last occurrence of 'Icharacters in the string.

String^ string1 = "Hello World"; Console::WriteLine(string1->LastIndexOf('l'));
string string1 = "Hello World";Console.WriteLine(string1.LastIndexOf('l'));
Dim string1 As String = "Hello World!"Console.WriteLine(string1.LastIndexOf("l"))

This example shows9on the console.

Both methods are useful when combined withstrict deletionmethod. you can use itindexthelast indexThe method retrieves the position of a character and then passes that position toeliminatemethod of deleting one or more words beginning with this character.

See also

  • Best practices for working with strings in .NET
  • basic string manipulation
  • Perform crop-sensitive string operations
  • Sort the weight table- Used by .NET Framework and .NET Core 1.0-3.1 on Windows
  • Table of standard Unicode taxonomy elements- Used by .NET 5 on all platforms and .NET Core on Linux and macOS

References

Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated: 28/07/2023

Views: 5973

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.