티스토리 뷰
C# 문자열 비교 3::Compare()
1. 사용 패턴
가) public static int Compare(string strA, string strB);
나) public static int Compare(string strA, string strB, StirngComparison comparisonType);
다) public static int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture);
라) public static int Compare(string strA, string strB, CultureInfo culture, ComparisonOptions options);
마) public static int Compare(string strA, int indexA, string strB, int indexB, int length);
바) public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool igonreCase);
사) public static int Compare(string strA, int indexA, string strB, int intdexB, int length, StringComparison comparisonType);
strA, strB: 비교할 문자열
comparisonType: StringComparison 열거형 자료 타입의 한 멤버 유형
indexA, indexB: 각 문자열의 시작 위치
가)의 용법
public static int Compare(string strA, string strB);
- strA, strB 두 문자열 간의 어휘 관계를 나타내며 strA가 strB보다 작으면 음수, 같으면 0, 크면[1]
양수를 정수형으로 반환한다.
namespace comparsion
{
class Program
{
static void Main(string[] args)
{
string str1 = "Korea";
string str2 = "korea";
Console.WriteLine("str1과 str2를 비교한 결과는 {0}입니다.",
string.Compare(str1, str2));
Console.ReadLine();
}
}
}
결괏값: str1과 str2를 비교한 결과는 1입니다.
- 결괏값이 1이 나오는 이유는 소문자 ‘k’가 대문자 ‘K’보다 먼저 나타나기 때문에 str1이 str2보다 크다고 해석한 것이다.[2]
나)의 용법
public static int Compare(string strA, string strB, StirngComparison comparisonType);
namespace comparsion
{
class Program
{
static void Main(string[] args)
{
string str1 = "Korea";
string str2 = "korea";
Console.WriteLine("str1과 str2의 부분을 비교한 결과는 {0}입니다.", string.Compare(str1, str2, StringComparison.CurrentCultureIgnoreCase));
Console.ReadLine();
}
}
}
결괏값: str1과 str2의 부분을 비교한 결과는 0입니다.
- 대소문자 차이를 무시하므로 비교결과가 같다.
마)의 용법
public static int Compare(string strA, int indexA, string strB, int indexB, int length);
- strA는 indexA에서, strB는 indexB에서 시작되며 정의된 길이(length)만큼의 문자열 부분을 비교하여
strA의 부분이 strB 부분의 문자열보다 작으면 음수, 같으면 0, 작으면 양수를 반환한다.
namespace comparsion
{
class Program
{
static void Main(string[] args)
{
string str1 = "Korea";
string str2 = "korea";
Console.WriteLine("str1과 str2의 부분을 비교한 결과는 {0}입니다.",
string.Compare(str1, 1, str2, 0, 3));
Console.ReadLine();
}
}
}
결괏값: str1과 str2의 부분을 비교한 결과는 1입니다.
'C#' 카테고리의 다른 글
C# 문자열 비교 2::Equals() (0) | 2016.02.02 |
---|---|
C# 문자열 비교 1 (0) | 2016.02.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- marsedit
- tistoy. drafts 4
- HTML5
- markdown
- 하
- 얼불노
- c#
- STYLES
- compare()
- HTML 5
- attribute
- html
- 스타일
- 속성
- notpad
- 마틴
- equals()
- formatting
- 까마귀의 향연
- tistory
- compareTo()
- testedit
- Element
- paragraphs
- headings
- String
- html. w3s
- 인용
- w3s
- 서식
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함