C#에서 문자열을 비교하려면 compare() 메서드를 사용합니다. 두 문자열을 비교하여 다음 정수 값을 반환합니다. -
If str1 is less than str2, it returns -1. If str1 is equal to str2, it returns 0. If str1 is greater than str2, it returns 1.
String.compare() 메서드에서 두 문자열을 설정하고 비교 -
string.Compare(string1, string2);
예시
다음 코드를 실행하여 C#에서 두 문자열을 비교할 수 있습니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
string string1 = null;
string string2 = null;
string1 = "amit";
string2 = "Amit";
int myOutput = 0;
myOutput = string.Compare(string1, string2);
Console.WriteLine(myOutput.ToString());
Console.ReadLine();
}
}
} 출력
-1