首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > .NET字串起訖計算
【标  题】:.NET字串起訖計算
【关键字】:.NET
【来  源】:http://blog.csdn.net/marvinhong/archive/2006/10/18/1340126.aspx

.NET字串起訖計算

例如有一dataset中的某一字串為6A0001,6A0002,6A0003,6A0007,6B0001,6B0002...代表箱號.若需要顯示為6A0001~6A0003;6A0007;6B0001~6B0002,代碼如下

    /// <summary>
        
/// 从字符串中的结尾取得指定长度无符号数字。
        
/// </summary>
        
/// <param name="text">The text.</param>
        
/// <param name="length">要取得数字的位数,如果为0则表示不知道位数将取得结尾的所有数字部分,在匹配成功后会将
        
/// 此变量置为实际取得的数字。</param>
        
/// <param name="prefixText">存放除去取出数字后字符串的变量。</param>
        
/// <param name="number">存放结果数字。</param>
        
/// <example>
        
/// string s = "MB2A100105";<br/>
        
/// string s1;<br/>
        
/// ulong l;<br/>
        
/// GetNumberOfTextEnd(s, 0, out s1, out l); //l=100005<br/>
        
/// GetNumberOftestEnd(s, 6, out s1, out l); //l=100005<br/>
        
/// GetNumberOfTextEnd(s, 3, out s1, out l); //l=105<br/>
        
/// </example>
        
/// <returns></returns>

        public static void GetNumberOfTextEnd(string text, ref int length, out string prefixText, out ulong number)
        
{
            
string expresssion = @"[0-9]{{{0}}}$";
            
if (length <= 0)
            
{
                expresssion 
= string.Format(expresssion, "0,");
            }

            
else
            
{
                expresssion 
= string.Format(expresssion, length);
            }


            Match match 
= Regex.Match(text, expresssion, RegexOptions.RightToLeft);
            
if (match.Success)
            
{
                number 
= ulong.Parse(match.Value);
                prefixText 
= text.Substring(0, match.Index);
                length 
= match.Length;
            }

            
else
            
{
                
throw new HanRocException("NFW-0025", text); //无法在文本结尾找到数字。
            }

        }


if (ds.Tables[0].Rows.Count>0)
            
{
                boxQty 
= ds.Tables[0].Rows.Count;
                
string currentBox = ds.Tables[0].Rows[0]["OUTER_BOX"].ToString();
                
int length = -1;
                
ulong beginNum, endNum;
                
string prefixText1 = "", prefixText2 = "", nextBox = "";
                boxRange 
= currentBox;

                
for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                
{
                    length 
= 0;
                    Helper.GetNumberOfTextEnd(currentBox, 
ref length, out prefixText1, out beginNum);
                    nextBox 
= ds.Tables[0].Rows[i]["OUTER_BOX"].ToString();
                    length 
= 0;
                    Helper.GetNumberOfTextEnd(nextBox, 
ref length, out prefixText2, out endNum);
                    
if (prefixText1 != prefixText2)
                    
{
                        boxRange 
+= " ; " + nextBox;
                        currentBox 
= nextBox;
                    }

                    
else
                    
{
                        
if (beginNum + 1 == endNum)
                        
{
                            
if (i == ds.Tables[0].Rows.Count - 1)
                            
{
                                boxRange 
+= " ~ " + nextBox;
                                
continue;
                            }

                            
else
                            
{
                                
string box = "", prefix = "";
                                
ulong num;
                                length 
= 0;
                                box 
= ds.Tables[0].Rows[i + 1]["OUTER_BOX"].ToString();
                                Helper.GetNumberOfTextEnd(box, 
ref length, out prefix, out num);
                                
if (prefix != prefixText2)
                                
{
                                    boxRange 
+= " ~ " + nextBox;
                                    currentBox 
= nextBox;
                                }

                                
else
                                
{
                                    
if (endNum + 1 < num)
                                    
{
                                        boxRange 
+= " ~ " + nextBox;
                                        currentBox 
= nextBox;
                                    }

                                    
else
                                    
{
                                        currentBox 
= nextBox;
                                        
continue;
                                    }

                                }

                            }

                        }

                        
else if (beginNum + 1 < endNum)
                        
{
                            boxRange 
+= " ; " + nextBox;
                            currentBox 
= nextBox;
                        }

                        
else
                        
{
                            currentBox 
= nextBox;
                            
continue;
                        }

                    }

                }

            }
一起学习.net(之C#基础篇):【上一篇】
visio 对.NET源代码的反向工程:【下一篇】
【相关文章】
  • 一起学习.net(之C#基础篇)
  • Asp.Net中实现二级或多级域名(URL重写)
  • asp.net中关于遍历页面控件并对文本框初化的最正确代码
  • [原创]如何在Delphi中调用.Net编写的DLL中的函数(Function)、过程(Procedure)
  • Asp.net中创建Auto Complete Text Box 概述
  • .Net向SQL Server数据库写入文件
  • c#.net常用的小函数和方法集
  • 如何在Delphi中调用.Net编写的DLL中的函数(Function)、过程(Procedure)
  • 關於.net的加解密密碼的代碼
  • 初探宏的强大,写一个统计VB.NET代码行数的宏(上)
  • 【随机文章】
  • JavaScript校验学习总结
  • web开发起步
  • 利用WSH修改注册表
  • 备份百度空间Blog的Python程序
  • 此篇写给城里普通收入的年轻人zz
  • 50个AJAX Tools and Frameworks
  • 实例讲解flashMTV制作全过程(6)
  • Metro DWDM应用前景浅析
  • 2002年4月全国计算机等级考试二级C语言试卷(含答案)
  • 用HTMLParser解析Html文件
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.