在 .NET 中,您可以使用 String.Normalize()
规范化(NFC、NFD、NFKC、NFKD)字符串并且有一个 Text.NormalizationForm
枚举。
在 .NET for Windows Store Apps 中,两者都不可用。我看过String
类和在 System.Text
和 System.Globalization
命名空间,但一无所获。
我错过了什么吗?如何在 Windows 应用商店应用中规范化字符串?
有谁知道为什么Normalize
方法不适用于商店应用程序?
最佳答案
正如您所指出的, Normalize
method在 String
class 上不可用在 Windows 商店应用程序上。
然而,这只是调用 NormalizeString
function在 Windows API 中。
更好的是,此功能在 the approved list of Win32 and COM API functions usable in Windows Store apps 中.
也就是说,您将做出以下声明:
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();
关于.net - 如何规范化字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14773128/