>  기사  >  백엔드 개발  >  C# 비교()

C# 비교()

PHPz
PHPz원래의
2024-09-03 15:18:14959검색

C#에서는 Compare() 메서드를 사용하여 두 문자열을 비교할 수 있습니다. 이 정수 값은 0보다 작거나, 0과 같거나, 0보다 클 수 있습니다. 주어진 두 문자열 중 정렬 순서에서 첫 번째 문자열이 두 번째 문자열보다 앞에 있고 반환 값이 0인 경우 반환 값은 0보다 작습니다. 두 문자열의 값이 동일하고 Compare() 메서드의 반환 값이 0보다 큰 경우 두 번째 문자열은 정렬 순서에서 첫 번째 문자열 뒤에 옵니다.

구문:

구문은 다음과 같습니다.

String.Compare(string1, string2);

여기서 string1은 두 번째 문자열 string2와 비교해야 하는 첫 번째 문자열입니다.

C# Compare() 작업

  • 정렬 순서에서 두 문자열을 비교하고 정렬 순서에서 첫 번째 문자열이 두 번째 문자열보다 앞에 있는지 또는 두 번째 문자열이 정렬 순서에서 첫 번째 문자열을 따르는지 확인해야 할 때마다 또는 두 문자열이 동일하면 C#의 Compare() 메서드를 사용합니다.
  • 두 문자열의 값이 동일하면 Compare() 메서드는 0을 반환합니다.
  • Compare() 메서드는 주어진 두 문자열 중 정렬 순서에 따라 첫 번째 문자열이 두 번째 문자열보다 앞에 있는 경우 0보다 작은 값을 반환합니다.

다음은 언급된 예입니다.

예시 #1

Compare() 메서드를 사용하여 두 문자열을 비교하는 방법을 보여주는 C# 프로그램

코드:

using System;
//a class called check is defined
public class check
{
//main method is called within which three string variables are defined to store three different strings
public static void Main(string[] args)
{
string string1 = "Welcome";
string string2 = "to";
string string3 = "C#";
//compare() method is used to compare two strings at a given time which returns an integer value less than zero if the first string precedes the second string in the sorting order or returns an integer value equal to zero if the first string is equal to the second string or returns an integer value greater than zero if the first string is followed by the second string in the sorting order
Console.WriteLine("The result of comparing the string1 and string2 is: {0}",string.Compare(string1,string2));
Console.WriteLine("The result of comparing the string2 and string3 is: {0}",string.Compare(string2,string3));
Console.WriteLine("The result of comparing the string3 and string1 is: {0}",string.Compare(string3,string1));
}
}

출력:

C# 비교()

설명:

  • Compare() 메서드는 두 문자열을 동시에 비교합니다. 이 메서드는 정렬 순서에서 첫 번째 문자열이 두 번째 문자열보다 앞에 있는 경우 0보다 작은 정수 값을 반환합니다. 첫 번째 문자열이 두 번째 문자열과 유사한 경우 0과 같은 정수 값을 반환합니다. 마지막으로 정렬 순서에서 첫 번째 문자열이 두 번째 문자열 다음에 오면 0보다 큰 정수 값을 반환합니다.

예시 #2

Compare() 메서드를 사용하여 두 문자열을 비교하는 방법을 보여주는 C# 프로그램

코드:

using System;
//a class called check is defined
public class check
{
//main method is called within which three string variables are defined to store three different strings
public static void Main(string[] args)
{
string string1 = "Learning is fun";
string string2 = "Learning is fun";
string string3 = "fun";
//compare() method is used to compare two strings at a given time which returns an integer value less than zero if the first string precedes the second string in the sorting order or returns an integer value equal to zero if the first string is equal to the second string or returns an integer value greater than zero if the first string is followed by the second string in the sorting order
Console.WriteLine("The result of comparing the string1 and string2 is: {0}",string.Compare(string1,string2));
Console.WriteLine("The result of comparing the string2 and string3 is: {0}",string.Compare(string2,string3));
Console.WriteLine("The result of comparing the string3 and string1 is: {0}",string.Compare(string3,string1));
}
}

출력:

C# 비교()

설명:

  • 위 프로그램에는 check라는 클래스가 정의되어 있습니다. 그런 다음 세 개의 다른 문자열을 저장하기 위해 세 개의 문자열 변수가 정의된 기본 메서드가 호출됩니다.

장점

장점은 아래와 같습니다.

  • 정렬 순서에 따른 문자열의 상대적 위치는 C#의 Compare() 메서드를 사용하여 확인할 수 있습니다.
  • 주어진 피연산자가 문자열이고 정렬 순서에서 한 문자열이 다른 문자열보다 앞에 있는지 아니면 다른 문자열 뒤에 오는지 알고 싶다면 Compare() 메서드는 C#에서 사용할 수 있는 가장 좋은 옵션 중 하나입니다.

결론

이 튜토리얼에서는 프로그래밍 예제와 출력을 통해 Compare() 메서드의 정의, 구문, 작동을 통해 C#에서 Compare() 메서드의 개념을 살펴보고 처리하는 동안 프로그램에서 Compare() 메서드를 사용하는 이점을 살펴보았습니다. 문자열로.

위 내용은 C# 비교()의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 사용자 컨트롤다음 기사:C# 사용자 컨트롤