roc*_*cks 5 c# asp.net string-conversion asp.net-core string-operations
日语 Windows 操作系统上的 C# 应用程序 - 将拉丁语显示为全角字符
我在上面的链接中引用了接受的答案,并使用下面的代码将日语字符串从全宽转换为半宽,但它返回相同的全宽字符串而不进行转换。
string userInput = "??????????";
string result = userInput.Normalize(NormalizationForm.FormKC);
Run Code Online (Sandbox Code Playgroud)
半宽的预期输出:?????????? 实际输出:?????????? (全屏宽度)
但是,即使上面的代码应该将全角字符串转换为半角,当我将半角字符串 (??????????) 传递给上面的代码时,它也会将其转换为全角形式(????????????)。
我在这里做错了什么?
无论如何,如果我的字符串已经是半角,我不希望执行上面的代码。
如何检查字符串是半宽还是全宽?
根据这个文档,标准化方法按预期工作。它必须将字符转换为标准字符,这样二进制比较才能正确应用。
\n\n但是,如果您想要始终将全角字符转换为半角字符的自定义转换,则可以创建一个字典来将全角字符映射到半角字符。这个链接可能有助于创建此地图。
\n\n如果您想确保字符串是半角,那么如果它包含任何全角字符,则会被拒绝。创建一个所有全角字符(拉丁文和日文)的字符串,然后在全角字符串中查找要测试字符串的所有字符。
\n\n我写isHalfWidthString为此目的编写了方法,并添加了全角到半角转换器方法。我认为这可能会有所帮助:
public class FullWidthCharactersHandler\n {\n static Dictionary<char, char> fullWidth2halfWidthDic;\n static FullWidthCharactersHandler()\n {\n fullWidth2halfWidthDic = new Dictionary<char, char>();\n string fullWidthChars = "\xe3\x82\xa2\xe3\x82\xa4\xe3\x82\xa6\xe3\x82\xa8\xe3\x82\xaa\xe3\x82\xab\xe3\x82\xad\xe3\x82\xaf\xe3\x82\xb1\xe3\x82\xb3\xe3\x82\xb5\xe3\x82\xb7\xe3\x82\xb9\xe3\x82\xbb\xe3\x82\xbd\xe3\x82\xbf\xe3\x83\x81\xe3\x83\x84\xe3\x83\x86\xe3\x83\x88\xe3\x83\x8a\xe3\x83\x8b\xe3\x83\x8c\xe3\x83\x8d\xe3\x83\x8e\xe3\x83\x8f\xe3\x83\x92\xe3\x83\x95\xe3\x83\x98\xe3\x83\x9b\xe3\x83\x9e\xe3\x83\x9f\xe3\x83\xa0\xe3\x83\xa1\xe3\x83\xa2\xe3\x83\xa4\xe3\x83\xa6\xe3\x83\xa8\xe3\x83\xa9\xe3\x83\xaa\xe3\x83\xab\xe3\x83\xac\xe3\x83\xad\xe3\x83\xaf\xe3\x83\xb2\xe3\x83\xb3\xe3\x83\x83\xe3\x82\xa1\xe3\x82\xa3\xe3\x82\xa5\xe3\x82\xa7\xe3\x82\xa9\xe3\x83\xa3\xe3\x83\xa5\xe3\x83\xa7\xe3\x82\x99\xe3\x82\x9a\xe3\x83\xbc\xef\xbc\x90\xef\xbc\x91\xef\xbc\x92\xef\xbc\x93\xef\xbc\x94\xef\xbc\x95\xef\xbc\x96\xef\xbc\x97\xef\xbc\x98\xef\xbc\x99\xef\xbc\xa1\xef\xbc\xa2\xef\xbc\xa3\xef\xbc\xa4\xef\xbc\xa5\xef\xbc\xa6\xef\xbc\xa7\xef\xbc\xa8\xef\xbc\xa9\xef\xbc\xaa\xef\xbc\xab\xef\xbc\xac\xef\xbc\xad\xef\xbc\xae\xef\xbc\xaf\xef\xbc\xb0\xef\xbc\xb1\xef\xbc\xb2\xef\xbc\xb3\xef\xbc\xb4\xef\xbc\xb5\xef\xbc\xb6\xef\xbc\xb7\xef\xbc\xb8\xef\xbc\xb9\xef\xbc\xba\xef\xbd\x81\xef\xbd\x82\xef\xbd\x83\xef\xbd\x84\xef\xbd\x85\xef\xbd\x86\xef\xbd\x87\xef\xbd\x88\xef\xbd\x89\xef\xbd\x8a\xef\xbd\x8b\xef\xbd\x8c\xef\xbd\x8d\xef\xbd\x8e\xef\xbd\x8f\xef\xbd\x90\xef\xbd\x91\xef\xbd\x92\xef\xbd\x93\xef\xbd\x94\xef\xbd\x95\xef\xbd\x96\xef\xbd\x97\xef\xbd\x98\xef\xbd\x99\xef\xbd\x9a";\n string halfWidthChars = "\xef\xbd\xb1\xef\xbd\xb2\xef\xbd\xb3\xef\xbd\xb4\xef\xbd\xb5\xef\xbd\xb6\xef\xbd\xb7\xef\xbd\xb8\xef\xbd\xb9\xef\xbd\xba\xef\xbd\xbb\xef\xbd\xbc\xef\xbd\xbd\xef\xbd\xbe\xef\xbd\xbf\xef\xbe\x80\xef\xbe\x81\xef\xbe\x82\xef\xbe\x83\xef\xbe\x84\xef\xbe\x85\xef\xbe\x86\xef\xbe\x87\xef\xbe\x88\xef\xbe\x89\xef\xbe\x8a\xef\xbe\x8b\xef\xbe\x8c\xef\xbe\x8d\xef\xbe\x8e\xef\xbe\x8f\xef\xbe\x90\xef\xbe\x91\xef\xbe\x92\xef\xbe\x93\xef\xbe\x94\xef\xbe\x95\xef\xbe\x96\xef\xbe\x97\xef\xbe\x98\xef\xbe\x99\xef\xbe\x9a\xef\xbe\x9b\xef\xbe\x9c\xef\xbd\xa6\xef\xbe\x9d\xef\xbd\xaf\xef\xbd\xa7\xef\xbd\xa8\xef\xbd\xa9\xef\xbd\xaa\xef\xbd\xab\xef\xbd\xac\xef\xbd\xad\xef\xbd\xae\xef\xbe\x9e\xef\xbe\x9f\xef\xbd\xb00123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";\n for (int i = 0; i < fullWidthChars.Length; i++)\n {\n fullWidth2halfWidthDic.Add(fullWidthChars[i], halfWidthChars[i]);\n }\n }\n\n public static bool isHalfWidthString(string toTestString)\n {\n bool isHalfWidth = true;\n foreach (char ch in toTestString)\n {\n if (fullWidth2halfWidthDic.ContainsKey(ch))\n {\n isHalfWidth = false;\n break;\n }\n }\n return isHalfWidth;\n }\n\n public static string convertFullWidthToHalfWidth(string theString)\n {\n StringBuilder sbResult = new StringBuilder(theString);\n for (int i = 0; i < theString.Length; i++)\n {\n if (fullWidth2halfWidthDic.ContainsKey(theString[i]))\n {\n sbResult[i] = fullWidth2halfWidthDic[theString[i]];\n }\n }\n return sbResult.ToString();\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n如需测试,请使用此链接。
\n\n我更新了要使用的代码Dictionary更好的性能。
| 归档时间: |
|
| 查看次数: |
2217 次 |
| 最近记录: |