You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity中C#特定变音符号替换代码失效问题求助

修复罗马尼亚语变音符号替换失效问题

问题根源是Unicode规范化差异:直接测试时用的是单个预组合Unicode字符(如ș对应U+015F),但从GetAnswer获取的字符串可能是分解形式(如s + U+0326 下方逗号组合字符)。两种形式视觉上完全一致,但string.Replace会将它们视为不同字符,导致替换失败。

修复后的代码

先将字符串规范化为预组合形式,再执行替换:

public string FaraDiacritice(string x)
{
    // 规范化为预组合Unicode形式,统一字符编码
    string normalizedStr = x.Normalize(NormalizationForm.FormC);
    return normalizedStr.Replace("ă", "a")
                       .Replace("î", "i")
                       .Replace("â", "a")
                       .Replace("ș", "s")
                       .Replace("ț", "t");
}

进阶兜底方案(若仍有问题)

如果规范化后仍存在替换失效,可额外处理分解形式的组合变音符号:

public string FaraDiacritice(string x)
{
    string normalizedStr = x.Normalize(NormalizationForm.FormC);
    string result = normalizedStr.Replace("ă", "a")
                               .Replace("î", "i")
                               .Replace("â", "a")
                               .Replace("ș", "s")
                               .Replace("ț", "t");
    // 移除残留的分解式变音符号(如下方逗号)
    result = result.Replace('\u0326', '');
    return result;
}

问题验证方法

可以通过打印字符编码确认输入字符串的实际形式:

string input = TextScript.instance.GetAnswer(Raspunsuri.instance.Q6[1]);
foreach (char c in input)
{
    Debug.Log($"字符: {c},Unicode编码: {(int)c:X4}");
}

比如分解形式的ș会输出两个字符:s(U+0073)和̦(U+0326),而预组合形式只会输出单个U+015F。

内容的提问来源于stack exchange,提问作者Vladutzu27

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 08:01:55