# 有谁能把这段代码重构一下吗？

0
[待解决问题]

```/// <summary>
/// 根据规则设置物流号
/// </summary>
/// <param name="expressCode"></param>
private void SetExpressCode(string expressCode)
{
string traceExpressNo = this.txtExpressNo.Text.Trim();
if (expressCode == "CNSFEDEX" || expressCode == "HKFEDEX" || expressCode == "SHSFEDEX"
|| expressCode == "CNFEDEX" || expressCode == "CNSFEDEXZY")
{
if (traceExpressNo.Length > 12)
{
traceExpressNo = traceExpressNo.Substring(0, 12);
}
}
else if (expressCode == "UPSNR")
{
if (traceExpressNo.Length > 26)
{
traceExpressNo = traceExpressNo.Substring(8, 26);
}
}
else if (expressCode == "DHLRM")
{
if (traceExpressNo.Length > 22)
{
traceExpressNo = traceExpressNo.Substring(8, 22);
}
}
else if (expressCode == "GBEXP")
{
if (traceExpressNo.Length == 19)
{
traceExpressNo = traceExpressNo.Substring(1, 18);
}
}
else if (expressCode == "GBEXP")
{
if (traceExpressNo.Length == 19)
{
traceExpressNo = traceExpressNo.Substring(1, 18);
}
}
else if (expressCode == "DEEXP")
{
if (traceExpressNo.Length == 28)
{
traceExpressNo = traceExpressNo.Substring(8, 14);
}
}
txtExpressNo.Text = traceExpressNo;
}```

0

0
```        /// <summary>
/// 根据规则设置物流号
/// </summary>
/// <param name="expressCode"></param>
private void SetExpressCode(string expressCode)
{
string traceExpressNo = this.txtExpressNo.Text.Trim();
int maxLength = 0;
int fixedLength = 0;
int beginIndex = 0;
int endIndex = 0;
switch(expressCode){
case "CNSFEDEX":
case "HKFEDEX":
case "SHSFEDEX":
case "CNFEDEX":
case "CNSFEDEXZY":
maxLength =  12;
beginIndex = 0;
endIndex = 12;
break;
case "UPSNR":
maxLength = 26;
beginIndex = 8;
endIndex = 26;
break;
case "DHLRM":
maxLength = 22;
beginIndex = 8;
endIndex = 22;
break;
case "GBEXP":
fixedLength = 19;
beginIndex = 1;
endIndex = 18;
break;
case "DEEXP":
fixedLength = 28;
beginIndex = 8;
endIndex = 14;
break;
default:
//TODO
}
int length = traceExpressNo.Length;
if((maxLength > 0 && length > maxLength) || (fixedLength > 0 && length == fixedLength)){
traceExpressNo = traceExpressNo.Substring(beginIndex, endIndex);
}
txtExpressNo.Text = traceExpressNo;```
rubekid | 园豆：206 (菜鸟二级) | 2015-04-02 14:14

0

switch 是可以的 。在此基础上 ，如果数字都是固定的话 可以用 model + 枚举  容易修改和拓展

akforsure | 园豆：248 (菜鸟二级) | 2015-04-02 14:27
0
```            string traceExpressNo = this.txtExpressNo.Text.Trim();
int beginIndex = 0;
int length = traceExpressNo.Length;
switch(expressCode){
case "CNSFEDEX":
case "HKFEDEX":
case "SHSFEDEX":
case "CNFEDEX":
case "CNSFEDEXZY":
if (traceExpressNo.Length > 12)
{
beginIndex = 0;
length = 12;
}
break;
case "UPSNR":
//类推
break;
case "DHLRM":
//类推
break;
case "GBEXP":
//类推
break;
case "DEEXP":
//类推
break;
}
txtExpressNo.Text = traceExpressNo.Substring(beginIndex, length);```
519740105 | 园豆：5810 (大侠五级) | 2015-04-02 15:08
0

0

```public delegate string StringHandler();
public class Class1
{
public static void SetExpressCode(string expressCode)
{
string traceExpressNo = "abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq";
var dic = new Dictionary<string, StringHandler>();
dic.Add("UPSNR", () => { return GetString(traceExpressNo, 26, 8, 26); });
dic.Add("DHLRM", () => { return GetString(traceExpressNo, 22, 8, 22); });
var str = dic[expressCode]();
}
static string GetString(string str, int length, int index, int length2)
{
if (str.Length > length)
{
str = str.Substring(index, length2);
}
return str;
}
}```
hubro | 园豆：98 (初学一级) | 2015-04-02 16:26

string traceExpressNo = "abcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopqabcdefghijklmnopq";

@422159763:

您需要登录以后才能回答，未注册用户请先注册