Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# String.IsNormalized Method
The String.IsNormalized() method in C# is used to indicate whether this string is in a particular Unicode normalization form. Unicode normalization ensures that text with the same meaning has a consistent binary representation, which is important for string comparison and text processing.
Syntax
The syntax is as follows −
public bool IsNormalized(); public bool IsNormalized(System.Text.NormalizationForm normalizationForm);
Parameters
normalizationForm − A System.Text.NormalizationForm enumeration value that specifies the Unicode normalization form to check against. The possible values are:
FormC − Canonical Decomposition followed by Canonical Composition
FormD − Canonical Decomposition
FormKC − Compatibility Decomposition followed by Canonical Composition
FormKD − Compatibility Decomposition
Return Value
Returns true if the string is normalized in the specified form; otherwise, false.
Using IsNormalized() with Default Form
When called without parameters, IsNormalized() checks for FormC normalization −
using System;
public class Demo {
public static void Main() {
string str1 = "Hello";
string str2 = "World";
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
bool res1 = str1.IsNormalized();
Console.WriteLine("String 1 is normalized (FormC): " + res1);
bool res2 = str2.IsNormalized();
Console.WriteLine("String 2 is normalized (FormC): " + res2);
}
}
The output of the above code is −
String 1 = Hello String 2 = World String 1 is normalized (FormC): True String 2 is normalized (FormC): True
Using IsNormalized() with Specific Forms
This example demonstrates checking normalization across different Unicode forms −
using System;
using System.Text;
public class Demo {
public static void Main() {
string str = "Café";
Console.WriteLine("String = " + str);
Console.WriteLine("Is normalized to FormC? = " +
str.IsNormalized(NormalizationForm.FormC));
Console.WriteLine("Is normalized to FormD? = " +
str.IsNormalized(NormalizationForm.FormD));
Console.WriteLine("Is normalized to FormKC? = " +
str.IsNormalized(NormalizationForm.FormKC));
Console.WriteLine("Is normalized to FormKD? = " +
str.IsNormalized(NormalizationForm.FormKD));
}
}
The output of the above code is −
String = Café Is normalized to FormC? = True Is normalized to FormD? = True Is normalized to FormKC? = True Is normalized to FormKD? = True
Practical Example with Accented Characters
This example shows how normalization affects strings with combined characters −
using System;
using System.Text;
public class Demo {
public static void Main() {
// Character é can be represented as single character or e + accent
string composed = "\u00e9"; // Single character é
string decomposed = "\u0065\u0301"; // e + combining acute accent
Console.WriteLine("Composed string: " + composed);
Console.WriteLine("Decomposed string: " + decomposed);
Console.WriteLine("Are they equal? " + composed.Equals(decomposed));
Console.WriteLine("\nNormalization check:");
Console.WriteLine("Composed FormC: " + composed.IsNormalized(NormalizationForm.FormC));
Console.WriteLine("Decomposed FormC: " + decomposed.IsNormalized(NormalizationForm.FormC));
Console.WriteLine("Composed FormD: " + composed.IsNormalized(NormalizationForm.FormD));
Console.WriteLine("Decomposed FormD: " + decomposed.IsNormalized(NormalizationForm.FormD));
}
}
The output of the above code is −
Composed string: é Decomposed string: é Are they equal? False Normalization check: Composed FormC: True Decomposed FormC: False Composed FormD: False Decomposed FormD: True
Conclusion
The String.IsNormalized() method is essential for Unicode text processing, allowing you to check if strings are in specific normalization forms. This is crucial for proper string comparison and ensuring consistent text representation in applications dealing with international characters.
