C#의 String.IsNormalized() 메서드는 해당 문자열이 특정 유니코드 정규화(Normalization) 형식을 따르고 있는지 여부를 확인하는 데 사용됩니다. 문자열 비교나 검색 시 인코딩 문제를 방지하려면 정규화 상태를 점검하는 것이 중요합니다.
문법
IsNormalized() 메서드는 두 가지 오버로드 형태로 제공됩니다.
public bool IsNormalized(); public bool IsNormalized(System.Text.NormalizationForm normalizationForm);
매개변수 normalizationForm은 검사할 유니코드 정규화 형식을 나타내며, FormC(완전형 조합), FormD(완전형 분해), FormKC(KD 호완성 조합), FormKD(호완성 분해) 중 하나를 지정할 수 있습니다. 매개변수 없이 호출하면 기본적으로 FormC 기준으로 검사합니다.
예제 1: 기본 사용법
먼저 간단한 예제를 살펴보겠습니다.
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Ryan";
string str2 = "Matt";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("HashCode of String 1 = " + str1.GetHashCode());
Console.WriteLine("Index of character 'k' in str1 = " + str1.IndexOf("k"));
Console.WriteLine("\nString 2 = " + str2);
Console.WriteLine("HashCode of String 2 = " + str2.GetHashCode());
Console.WriteLine("Index of character 'k' in str2 =" + str2.IndexOf("k"));
bool res1 = str1.Contains(str2);
res1 = str1.IsNormalized();
Console.WriteLine("\nThe str1 is in normalized form = " + res1);
bool res2 = str1.Contains(str2);
res2 = str2.IsNormalized();
Console.WriteLine("The str2 is in normalized form = " + res2);
}
}
실행 결과
위 코드를 실행하면 다음과 같은 출력이 생성됩니다.
String 1 = Ryan HashCode of String 1 = 1580592915 Index of character 'k' in str1 = -1 String 2 = Matt HashCode of String 2 = -1920007383 Index of character 'k' in str2 = -1 The str1 is in normalized form = True The str2 is in normalized form = True
두 문자열 모두 기본 정규화 형식(FormC)을 만족하므로 True가 반환된 것을 확인할 수 있습니다.
예제 2: 다양한 정규화 형식 지정하기
이번에는 매개변수를 사용하여 여러 정규화 형식을 개별적으로 검사해 보겠습니다.
using System;
using System.Text;
public class Demo {
public static void Main(String[] args) {
string str1 = "Imagine Dragons";
string str2 = "Imagine";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("HashCode of String 1 = " + str1.GetHashCode());
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("HashCode of String 2 = " + str2.GetHashCode());
Console.WriteLine("String 1 is equal to String 2: {0}", str1.Equals(str2));
Console.WriteLine("str1 is normalized to form C? = {0}",
str1.IsNormalized(NormalizationForm.FormC));
Console.WriteLine("str2 is normalized to form C? = {0}", (str2.IsNormalized(NormalizationForm.FormC)));
Console.WriteLine("str1 is normalized to form D? = {0}", str1.IsNormalized(NormalizationForm.FormD));
Console.WriteLine("str2 is normalized to form D? = {0}", str2.IsNormalized(NormalizationForm.FormD));
Console.WriteLine("str1 is normalized to form KC? = {0}", str1.IsNormalized(NormalizationForm.FormKC));
Console.WriteLine("str2 is normalized to form KC? = {0}", str2.IsNormalized(NormalizationForm.FormKC));
}
}
실행 결과
위 코드를 실행하면 다음과 같은 출력이 생성됩니다.
String 1 = Imagine Dragons HashCode of String 1 = -1546868095 String 2 = Imagine HashCode of String 2 = -1414695254 String 1 is equal to String 2: False str1 is normalized to form C? = True str2 is normalized to form C? = True str1 is normalized to form D? = True str2 is normalized to form D? = True str1 is normalized to form KC? = True str2 is normalized to form KC? = True
영문 알파벳으로만 구성된 문자열은 모든 정규화 형식에 대해 이미 정규화된 상태이기 때문에 전부 True가 반환됩니다. 반면 한글 자모 분해 문자, 악센트가 포함된 프랑스어·독일어 문자처럼 결합 문자를 포함하는 경우에는 정규화 형식에 따라 결과가 달라질 수 있습니다.
정리
IsNormalized(): 문자열이 기본 정규화 형식(FormC)인지 확인IsNormalized(NormalizationForm): 특정 정규화 형식(C, D, KC, KD) 준수 여부 확인- 문자열을 정규화하려면
Normalize()메서드를 함께 활용하면 됩니다.