例如有一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;
}
}
}
}