C# String IsNormalized()

The IsNormalized() method in C# is used to determine whether a string is in a normalized form according to the specified normalization form. The method returns a Boolean value indicating whether the string is normalized or not.

Here’s the syntax of the IsNormalized() method:

public bool IsNormalized(NormalizationForm normalizationForm);

The normalizationForm parameter specifies the normalization form to use for the comparison. It is an enumeration of the NormalizationForm type, which defines different normalization forms such as FormC, FormD, FormKC, and FormKD. These forms represent different ways of normalizing Unicode text.

Here’s an example usage of the IsNormalized() method:

string str1 = "á";
string str2 = "á";
string str3 = "a\u0301";

bool isNormalized1 = str1.IsNormalized(NormalizationForm.FormC);
bool isNormalized2 = str2.IsNormalized(NormalizationForm.FormC);
bool isNormalized3 = str3.IsNormalized(NormalizationForm.FormC);

Console.WriteLine(isNormalized1);  // Output: True
Console.WriteLine(isNormalized2);  // Output: True
Console.WriteLine(isNormalized3);  // Output: False

In this example, str1 and str2 are both “á”, but str3 is “a\u0301” (the letter “a” followed by combining acute accent). The IsNormalized() method with NormalizationForm.FormC checks if the strings are normalized using the canonical decomposition, followed by the canonical composition. As a result, isNormalized1 and isNormalized2 both return true, indicating that the strings are normalized, while isNormalized3 returns false because the combining acute accent should be normalized to the precomposed “á”.

Parameters:

It does not take any parameter.

Return:

It returns boolean.

C# String IsNormalized() Method Example:

Certainly! Here’s an example that demonstrates the usage of the IsNormalized() method in C#:

using System;
using System.Text;

class Program
{
    static void Main()
    {
        string str1 = "Hello";
        string str2 = "H\u0065llo";
        string str3 = "Héllö";
        string str4 = "Héllö";

        Console.WriteLine(str1.IsNormalized());  // Output: True
        Console.WriteLine(str2.IsNormalized());  // Output: True
        Console.WriteLine(str3.IsNormalized());  // Output: False
        Console.WriteLine(str4.IsNormalized());  // Output: True
    }
}

In this example, we have four strings:

  • str1 is a simple ASCII string, which is already in the normalized form. The IsNormalized() method returns true.
  • str2 is constructed using a Unicode escape sequence to represent the letter ‘e’. However, it represents the same content as str1. The IsNormalized() method still returns true because it disregards the specific representation and focuses on the content.
  • str3 contains accented characters, ‘é’ and ‘ö’, which are not in the normalized form. The IsNormalized() method returns false.
  • str4 is the same as str3, but it is already in the normalized form. The IsNormalized() method returns true.

The IsNormalized() method checks whether a string is in its normalized form, regardless of the specific representation or encoding used.