在Axapta中没有找到类似于C#中的split函数,在SQL Server中也没有类似的函数,当初写SQL函数的时候用了SQL Server中的系统函数charindex和substr来实现这样的功能,于是首先想到了用函数strFind和subStr来实现split函数.下列代码可以分解一个字符串并打印:
static void split(Args _args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
int position;
str srcStr;
;
srcStr = "AA,BB,CC,";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while(true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
position = strFind(srcStr,",",1,strlen(srcStr));
if(!position)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
print(srcStr);
break;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
print(subStr(srcStr,1,position-1));
srcStr = subStr(srcStr,position+1,strlen(srcStr)-position);
}
pause;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
上面的代码显得臃肿并且很容易出错,因为要考虑边界情况,如果用Axapta中的Container实现这个功能就优雅得多,代码如下:
static void Split2(Args _args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Container c;
str srcStr;
int i;
;
srcStr = "AA,BB,CC,";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
c = Global::str2con(srcStr,",");
for(i=1;i<=conlen(c);i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
print(conpeek(c,i));
}
pause;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
当然如果查看str2con的代码就会知道,str2con也是用了strScan和substr对字符串进行分析处理,不过眼不见心不烦,不是吗?呵呵.另外str2con用函数match判断了字符串如果可以转换成数字,就会把字符串转换成数字,这点不够可爱,可以自己写一个str2conXpp之类的,把这个match去掉.