C #’s string comparison

Time:2021-10-16
catalogue
  • 1、 Function method
  • 2、 Compare method
  • 3、 CompareTo method
  • 4、 Equals method
  • 5、 Compareordinal method
  • summary

ASCII code – Baidu Encyclopedia

Microsoft official c# function document

1、 Function method

Function method Return value
String. Compare (string 1, string 2) Returns 0 when equal, 1 when the former is larger, and – 1 when the latter is larger
String. Compareordinal (string 1, string 2) Returns 0 if equal, otherwise returns the ASCII value of the former minus the latter
String 1. CompareTo (string 2) Returns 0 when equal, 1 when the former is larger, and – 1 when the latter is larger
String 1. Equals (string 2) Returns true if equal, false otherwise

2、 Compare method

string.Compare(str1, str2)

Parameters:str1andstr2For two strings to compare

Return value: returns if the two are equal0, ifstr1 > str2return1, ifstr1 < str2return-1

Note: comparing strings does not compare the length of strings, but compares the size of characters one by one from the first character. If the size has been separated, the comparison ends. If it is equal, continue to compare the next character until the size is separated or the end of the string is reached.

在这里插入图片描述

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1, str2;
            /*
            Example 1 (STR1 < STR2): because the first character of STR1 is a, which is smaller than B, the result will directly return - 1 without comparing subsequent values
            */
            str1 = "ABC";
            str2 = "B";
            Console. Writeline ("example 1: {0}", string. Compare (STR1, STR2));
            /*
            Example 2 (STR1 > STR2): because STR2 has only one character a, STR1 has extra characters after comparing the first character, and the result will return 1
            */
            str1 = "ABC";
            str2 = "A";
            Console. Writeline ("example 2: {0}", string. Compare (STR1, STR2));
            /*
            Example 3 (STR1 < STR2): ASCII codes are not compared during comparison, and lowercase letters are smaller than uppercase letters by default
            */
            str1 = "a";
            str2 = "A";
            Console. Writeline ("example 3: {0}", string. Compare (STR1, STR2));
        }
    }
}

3、 CompareTo method

str1.CompareTo(str2)

Parameters:str1andstr2For two strings to compare

Return value: returns if the two are equal0, ifstr1 > str2return1, ifstr1 < str2return-1

be careful:CompareToMethods andCompareThe method is similar,CompareTo()Method compares the instance object itself with the target string. Comparing strings does not compare the length of strings, but compares the size of characters one by one from the first character. If the size has been separated, the comparison ends. If it is equal, continue to compare the next character until the size is separated or the end of the string is reached.

在这里插入图片描述

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1, str2;
            /*
            Example 1 (STR1 < STR2): because the first character of STR1 is a, which is smaller than B, the result will directly return - 1 without comparing subsequent values
            */
            str1 = "ABC";
            str2 = "B";
            Console. Writeline ("example 1: {0}", STR1. CompareTo (STR2));
            /*
            Example 2 (STR1 > STR2): because STR2 has only one character a, STR1 has extra characters after comparing the first character, and the result will return 1
            */
            str1 = "ABC";
            str2 = "A";
            Console. Writeline ("example 2: {0}", STR1. CompareTo (STR2));
            /*
            Example 3 (STR1 < STR2): ASCII codes are not compared during comparison, and lowercase letters are smaller than uppercase letters by default
            */
            str1 = "a";
            str2 = "A";
            Console. Writeline ("example 3: {0}", STR1. CompareTo (STR2));
        }
    }
}

4、 Equals method

str1.Equals(str2)

Parameters:str1andstr2For two strings to compare

Return value: returns if the two are equalTrue, otherwise returnFalse

在这里插入图片描述

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1, str2;
            /*
            Example 1 (STR1 = = STR2): returns true directly when equal
            */
            str1 = "ABC";
            str2 = "ABC";
            Console. Writeline ("example 1: {0}", STR1. Equals (STR2));
            /*
            Example 2 (STR1! = STR2): Returns FALSE if not equal
            */
            str1 = "ABC";
            str2 = "ACB";
            Console. Writeline ("example 2: {0}", STR1. Equals (STR2));
        }
    }
}

5、 Compareordinal method

string.CompareOrdinal(str1, str2)

Parameters:str1andstr2For two strings to compare

Return value: returns if the two are equal0, otherwise returnstr1subtractstr2bothASCIICode difference

在这里插入图片描述

using System;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1, str2;
            /*
            Example 1 (STR1 = = STR2): returns 0 directly when equal
            */
            str1 = "ABC";
            str2 = "ABC";
            Console. Writeline ("example 1: {0}", string. Compareordinal (STR1, STR2));
            /*
            Example 2 (STR1 > STR2): the ASCII code of B is 66, the ASCII code of a is 65, and the ASCII code value of B minus a is 1
            */
            str1 = "B";
            str2 = "A";
            Console. Writeline ("example 2: {0}", string. Compareordinal (STR1, STR2));
            /*
            Example 3 (STR1 > STR2): the ASCII code of a is 97, the ASCII code of a is 65, and the ASCII code value of a minus a is 32
            */
            str1 = "a";
            str2 = "A";
            Console. Writeline ("example 3: {0}", string. Compareordinal (STR1, STR2));
            /*
            Example 4 (STR1 > STR2): if there are multiple characters and they are not equal, the difference between the ASCII codes of the first characters of the two strings is returned, that is, 32
            */
            str1 = "abcd";
            str2 = "ABCD";
            Console. Writeline ("example 4: {0}", string. Compareordinal (STR1, STR2));
        }
    }
}

summary

That’s all for this article. I hope it can help you and pay more attention to developeppaer!

Recommended Today

OC basis

IOS development interview essential skills chart.png What are objects and what are the objects in OC? An object is an instance of a class; Is an instance created through a class, which is generally called an instance object; Common objects in OC include instance objects, class objects, and metaclass objects; What is a class? What […]