你几乎描述了你的问题的答案。为每个数字定义一个固定宽度。
int iShardId = 12; // Fixed width of 5
int iTableTypeId = 840; // Fixed width of 5
long lIncremental = 967295; // Fixed width of 10
string sMyId = String.Concat(iShardId.ToString("00000"), iTableTypeId.ToString("00000"), lIncremental.ToString("0000000000"));
然后,您可以稍后(通过 iHttpModule 或其他)使用 RegEx 解析字符串:
RegEx rMyText = new RegEx(@"/(?<shard>[0-9]{5})(?<table>[0-9]{5})(?<inc>[0-9]{10})/?$");
Match mMyValues = rMyText.Match(Request.Url.AbsolutePath);
if (mMyValues.Success) {
int iShardId = Convert.ToInt32(mMyValues["shard"].Value);
int iTableTypeId = Convert.ToInt32(mMyValues["table"].Value);
long lIncremental = Convert.ToInt64(mMyValues["inc"].Value);
}
else {
//The input didn't match
}
RegEx 旨在作为解析数字的示例,但显然取决于您计划如何实施,您应该调整它以确保输入限制为您期望的值,方法是使用开始/终止斜杠或字符串结尾($)。