关于用递归算法按[千分位]拆分字符串的问题 请高人指点比如 把12345678要用递归拆分成12,345,678 12345 拆分成 12,345就是按找千分位来拆分.先行谢过了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string resultStr = string.Empty;public void Sub(string str){ if(str.Length>0) { if(str.Length<3) { resultStr = str +","+ resultStr; } else { resultStr = resultStr!=string.Empty?str.Substring(str.Length-3,3)+","+resultStr:str.Substring(str.Length-3,3); Sub(str.Substring(0,str.Length-3)); } }} 你这是拆分还是格式化?如果只是格式化,使用string.Format,或者int.Parse方法,一条语句就搞定了。 哈哈,一楼的算法有bugstring resultStr = string.Empty;public void Sub(string str){ if(str.Length>0) { if(str.Length<3) { resultStr = resultStr!=string.Empty ? str +","+ resultStr : str; } else { resultStr = resultStr!=string.Empty ? str.Substring(str.Length-3,3)+","+resultStr : str.Substring(str.Length-3,3); Sub(str.Substring(0,str.Length-3)); } }} string ss = "1234567"; int cnt = ss.Length%3; if (cnt == 1) { ss = "00" + ss; } else if (cnt == 2) { ss = "0" + ss; } string ResultString = null; try { ResultString = Regex.Replace(ss, "(\\d{3})", "$1,"); } catch (ArgumentException ex) { // Syntax error in the regular expression } ResultString = ResultString.TrimEnd(",".ToCharArray()); ResultString = ResultString.TrimStart("0".ToCharArray()); 我的确实有BUG.你似乎将他完善了. 请编译高手进,某个语句对服务器处理速度的问题 谁能给个用C#从SQL 2005中导出数据到excel文档的例子?非常感谢! 请教高手,关于html转word的问题 借地方问下VS2005窗体设计器消失了. C#winform和WEB的通信方式有那些? 用过advanced installer安装包制作工具的进来看一下 小问题2个 请问大家做过自己的共享软件么?请说说名称,功能。如果出售,你们认为自己的软件价格是多少? 在DLL接口中,申明为ANSI的String如何取得返回值(传址) c# 键盘钩子的问题 一直为曾解决的问题 在DataTable中,把数据库中的英文字段转换成中文
public void Sub(string str)
{
if(str.Length>0)
{
if(str.Length<3)
{
resultStr = str +","+ resultStr;
}
else
{
resultStr = resultStr!=string.Empty?str.Substring(str.Length-3,3)+","+resultStr:str.Substring(str.Length-3,3);
Sub(str.Substring(0,str.Length-3));
}
}
}
{
if(str.Length>0)
{
if(str.Length<3)
{
resultStr = resultStr!=string.Empty ? str +","+ resultStr : str;
}
else
{
resultStr = resultStr!=string.Empty ? str.Substring(str.Length-3,3)+","+resultStr : str.Substring(str.Length-3,3);
Sub(str.Substring(0,str.Length-3));
}
}
}
int cnt = ss.Length%3;
if (cnt == 1)
{
ss = "00" + ss;
}
else if (cnt == 2)
{
ss = "0" + ss;
}
string ResultString = null;
try
{
ResultString = Regex.Replace(ss, "(\\d{3})", "$1,");
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
ResultString = ResultString.TrimEnd(",".ToCharArray());
ResultString = ResultString.TrimStart("0".ToCharArray());