- article
.NET provides several methods of comparing string values. The table below shows and describes the price comparison methods.
method name | use |
---|---|
strict comparison | Compares the values of two strings. Returns an integer value. |
String.ComparisonOrdinal | It compares two strings without considering the local culture. Returns an integer value. |
String.CompareTo | Compares the current String object to another String. Returns an integer value. |
String.StartsWith | Determines whether the string starts with the passed string. Returns a Boolean value. |
String.EndsWith | Determines whether the string ends with the passed string. Returns a Boolean value. |
string. contains | Determines whether a character or string appears in another string. Returns a Boolean value. |
strictly equal to | Determines if two strings are equal. Returns a Boolean value. |
string index | Returns the index position of a character or string, starting at the beginning of the string you're looking at. Returns an integer value. |
String.LastIndexOf | Returns the index position of a character or string, starting from the end of the string you're looking at. Returns an integer value. |
Compare
method
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 value | Health status |
---|---|
negative integer | In sort order, the first string precedes the second. -the- The first string is |
0 | The first and second strings are the same. -the- Both strings are |
positive integer -or- 1 | In sort order, the first string comes after the second string. -the- The second string is |
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-1
on 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 comparison
method
ThisString.ComparisonOrdinalmethod compares two String objects regardless of locale. The return value of this method is the same as this oneCompare
methods 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 comparison
method 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-32
on the console.
compared to
method
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 1
Be againststrictly 2
Purpose.
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-1
on 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 comparison
method, 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 comparison
For methods of performing culture-sensitive and culture-insensitive comparisons, seeIt makes a rigorous, culture-sensitive comparison.
just
method
Thisstrictly equal toThe method can easily determine if two strings are equal. This case-sensitive method returns agenuine
theInaccurate
Boolean value. It can be used from existing classes, as shown in the next example. The following example usesjust
method 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 showsgenuine
on 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 showsgenuine
on the console.
with. . Principle
andwith. . Exit
method
you can use itString.StartsWithmethod to determine if a string object starts with the same characters as another string containing. This case-sensitive method returnsgenuine
If the current string object starts with the passed string andInaccurate
if 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 showsgenuine
on 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. . Exit
method.
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 showsInaccurate
on the console.
index
andlast index
method
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 usesindex
Search method for the first occurrence of 'I
characters 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 shows2
on the console.
ThisString.LastIndexOfmethod corresponding tostring index
method, 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 index
search method for last occurrence of 'I
characters 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 shows9
on the console.
Both methods are useful when combined withstrict deletionmethod. you can use itindex
thelast index
The method retrieves the position of a character and then passes that position toeliminate
method 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