Dot-Net

如何規範化字元串?

  • January 7, 2015

String.Normalize()在 .NET 中,您可以使用 列舉來規範化(NFC、NFD、NFKC、NFKD)字元串Text.NormalizationForm

在 .NET for Windows Store Apps 中,兩者都不可用。我查看了String類和System.TextandSystem.Globalization命名空間,但什麼也沒找到。

我錯過了什麼嗎?如何規範化 Windows 應用商店應用程序中的字元串?

有誰知道為什麼該Normalize方法不適用於商店應用程序?

正如您所指出的,該Normalize方法在 Windows 商店應用程序的String中不可用。

但是,這只是呼叫Windows API 中的NormalizeString函式

更好的是,這個函式在 Windows 應用商店應用程序中可用的 Win32 和 COM API 函式的批准列表中

也就是說,您將做出以下聲明:

public enum NORM_FORM 
{ 
 NormalizationOther  = 0,
 NormalizationC      = 0x1,
 NormalizationD      = 0x2,
 NormalizationKC     = 0x5,
 NormalizationKD     = 0x6
};

[DllImport("Normaliz.dll", CharSet = CharSet.Unicode, ExactSpelling = true,
   SetLastError = true)
public static extern int NormalizeString(NORM_FORM NormForm,
   string lpSrcString,
   int cwSrcLength,
   StringBuilder lpDstString,
   int cwDstLength);

然後你會這樣稱呼它:

// The form.
NORM_FORM form = ...;

// String to normalize.
string unnormalized = "...";

// Get the buffer required.
int bufferSize = 
   NormalizeString(form, unnormalized, unnormalized.Length, null, 0);

// Allocate the buffer.
var buffer = new StringBuilder(bufferSize);

// Normalize.
NormalizeString(form, unnormalized, unnormalized.Length, buffer, buffer.Length);

// Check for and act on errors if you want.
int error = Marshal.GetLastWin32Error();

引用自:https://stackoverflow.com/questions/14773128