D365FO中X++拆分全名未识别空格导致结果异常的问题求助
D365FO X++ 全名拆分问题排查与修复
核心问题分析
你遇到的问题本质是非ASCII空格/特殊空白字符未被正确替换为普通空格,导致后续基于空格的拆分逻辑失效,最终把整个字符串判定为lastName。
排查步骤
- 验证替换后的字符串实际内容:在代码中加入调试输出,查看替换后
fullName的字符编码,确认是否存在未被替换的特殊空白字符。例如:str cleanedName = strReplace(strReplace(fullName, '\t', ' '), '\u00A0', ' '); // 原替换逻辑示例 info(strFmt("Cleaned name: %1, Space char code: %2", cleanedName, asc(subStr(cleanedName, 3, 1)))); // 检查中间空格的编码 - 确认修剪逻辑是否正确:
strTrim仅去除首尾普通空格,若首尾是特殊空白字符,修剪后仍会保留,影响后续拆分。
修复方案
方案1:全面替换所有Unicode空白字符
通过strReplace覆盖常见非ASCII空白字符,再进行修剪和拆分:
public static void splitFullName(str _fullName, str &_firstName, str &_middleName, str &_lastName) { str cleanedName = _fullName; // 替换所有常见非ASCII空白字符为普通空格 cleanedName = strReplace(cleanedName, '\t', ' '); // 制表符 cleanedName = strReplace(cleanedName, '\u00A0', ' '); // 非断空格(NBSP) cleanedName = strReplace(cleanedName, '\u2000', ' '); // 表意空格 cleanedName = strReplace(cleanedName, '\u2001', ' '); // 全角空格 cleanedName = strReplace(cleanedName, '\u2002', ' '); // 半角空格 cleanedName = strReplace(cleanedName, '\u2003', ' '); // 三分之一空格 cleanedName = strReplace(cleanedName, '\u2004', ' '); // 四分之一空格 cleanedName = strReplace(cleanedName, '\u2005', ' '); // 五分之一空格 cleanedName = strReplace(cleanedName, '\u2006', ' '); // 六分之一空格 cleanedName = strReplace(cleanedName, '\u2007', ' '); // 数字空格 cleanedName = strReplace(cleanedName, '\u2008', ' '); // 标点空格 cleanedName = strReplace(cleanedName, '\u2009', ' '); // 窄空格 cleanedName = strReplace(cleanedName, '\u200A', ' '); // 细空格 cleanedName = strReplace(cleanedName, '\u202F', ' '); // 窄非断空格 cleanedName = strReplace(cleanedName, '\u205F', ' '); // 中等数学空格 cleanedName = strReplace(cleanedName, '\u3000', ' '); // 中文全角空格 // 修剪首尾空格,并合并连续空格为单个空格 cleanedName = strTrim(strReplace(cleanedName, ' ', ' ')); while (strScan(cleanedName, ' ', 1, strLen(cleanedName)) != 0) { cleanedName = strReplace(cleanedName, ' ', ' '); } int firstSpacePos = strFind(cleanedName, ' ', 1, strLen(cleanedName)); int lastSpacePos = strFind(cleanedName, ' ', 1, strLen(cleanedName), true); if (firstSpacePos == 0) { _lastName = cleanedName; _firstName = ''; _middleName = ''; } else if (firstSpacePos == lastSpacePos) { _firstName = subStr(cleanedName, 1, firstSpacePos - 1); _lastName = subStr(cleanedName, firstSpacePos + 1, strLen(cleanedName) - firstSpacePos); _middleName = ''; } else { _firstName = subStr(cleanedName, 1, firstSpacePos - 1); _middleName = subStr(cleanedName, firstSpacePos + 1, lastSpacePos - firstSpacePos - 1); _lastName = subStr(cleanedName, lastSpacePos + 1, strLen(cleanedName) - lastSpacePos); } }
方案2:使用.NET正则批量替换空白字符
借助.NET正则表达式简化替换逻辑:
public static void splitFullName(str _fullName, str &_firstName, str &_middleName, str &_lastName) { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\s"); str cleanedName = regex.Replace(_fullName, " "); // 修剪并合并连续空格 cleanedName = strTrim(strReplace(cleanedName, ' ', ' ')); while (strScan(cleanedName, ' ', 1, strLen(cleanedName)) != 0) { cleanedName = strReplace(cleanedName, ' ', ' '); } // 拆分逻辑同方案1 int firstSpacePos = strFind(cleanedName, ' ', 1, strLen(cleanedName)); int lastSpacePos = strFind(cleanedName, ' ', 1, strLen(cleanedName), true); if (firstSpacePos == 0) { _lastName = cleanedName; _firstName = ''; _middleName = ''; } else if (firstSpacePos == lastSpacePos) { _firstName = subStr(cleanedName, 1, firstSpacePos - 1); _lastName = subStr(cleanedName, firstSpacePos + 1, strLen(cleanedName) - firstSpacePos); _middleName = ''; } else { _firstName = subStr(cleanedName, 1, firstSpacePos - 1); _middleName = subStr(cleanedName, firstSpacePos + 1, lastSpacePos - firstSpacePos - 1); _lastName = subStr(cleanedName, lastSpacePos + 1, strLen(cleanedName) - lastSpacePos); } }
验证结果
调用上述方法测试输入"mh ali hasan",输出应为:
_firstName = "mh"_middleName = "ali"_lastName = "hasan"
内容的提问来源于stack exchange,提问作者user4833581
相关产品推荐
相关产品推荐

