我有点凌乱的方法,它会从三个字符串中生成一个名字。这三个字符串可以为null。我根据给定的可用值检查每个可能的组合并创建一个字符串。结果名称中的每个变量都没有固定位置/布局,这取决于所有字符串的可用性。
我的方法现在看起来像这样:
private String GetName(string variable, string label, string dimension)
{
string result = String.Empty;
if (!String.IsNullOrEmpty(label) && !String.IsNullOrEmpty(variable) && !String.IsNullOrEmpty(dimension))
{
result = String.Format("{0} [{1}] ({2})", label, dimension, variable);
}
else if (!String.IsNullOrEmpty(label) && !String.IsNullOrEmpty(dimension))
{
result = String.Format("{0} [{1}]", label, dimension);
}
else if (!String.IsNullOrEmpty(label) && !String.IsNullOrEmpty(variable))
{
result = String.Format("{0} ({1})", label, variable);
}
else if (!String.IsNullOrEmpty(label))
{
result = String.Format("{0}", label);
}
else if (!String.IsNullOrEmpty(variable) && !String.IsNullOrEmpty(dimension))
{
result = String.Format("{0} [{1}]", variable, dimension);
}
else if (!String.IsNullOrEmpty(variable))
{
result = String.Format("{0}", variable);
}
return result;
}
有关如何改进此方法的任何建议(摆脱if / else结构)?
答案 0 :(得分:5)
检查顶部的空值,如果需要则更改为字符串,然后创建最终结果
var stringsToInclude = new List<string>();
if(!String.IsNullOrEmpty(label))
stringsToInclude.Add(label);
if(!String.IsNullOrEmpty(dimension))
stringsToInclude.Add(string.Format("[{0}]", dimension));
if(!String.IsNullOrEmpty(variable))
stringsToInclude.Add(string.Format("({0})", variable));
result = string.Join(" ", stringsToInclude);
答案 1 :(得分:0)
考虑到你不能在模板中使用空字符串或使用简单的合成,最好的办法是使用某种包含格式化功能的Func表(字典):
public class MyStringFormatterKey : IEquatable<MyStringFormatterKey>
{
private Boolean isVariableAbsent, isLabelAbsent, isDimensionAbsent;
public MyStringFormatterKey (String variable, String label, String dimension) :
this(String.IsNullOrEmpty(variable), String.IsNullOrEmpty(label), String.IsNullOrEmpty(dimension))
{ }
public MyStringFormatterKey (Boolean isVariableAbsent, Boolean isLabelAbsent, Boolean isDimensionAbsent)
{ this.isVariableAbsent = isVariableAbsent; this.isLabelAbsent= isLabelAbsent; this.isDimensionAbsent = isDimensionAbsent; }
public override Int32 GetHashCode()
{
return 100 * (isVariableAbsent ? (0) : (1)) + 10 * (isLabelAbsent ? (0) : (1)) + (isDimensionAbsent ? (0) : (1));
}
public bool Equals(MyStringFormatterKey other)
{
return (this.GetHashCode() == other.GetHashCode());
}
}
...
Dictionary<MyStringFormatterKey , Func<String, String, String, String>> formatterDictionary =
new Dictionary<MyStringFormatterKey , Func<String, String, String, String>>();
formatterDictionary.Add(new MyStringFormatterKey(false, true, false),
(variable, label, dimension) => String.Format("{0} [{1}]", variable, dimension));
...
Func<String, String, String, String> stringFormatter = formatterDictionary[new MyStringFormatterKey(variable, label, dimension)];
String result = stringFormatter(variable, label, dimension);
它可以更通用化(我的意思不仅仅是3个字符串),但最终这样的解决方案将能够处理任何可能的组合或格式化特性。
答案 2 :(得分:0)
如果我理解了你的逻辑,你也可以用这种方式缩短它:
private String GetName(string variable, string label, string dimension)
{
string[] all = { variable, label, dimension };
string[] notNull = all.Where(s => !string.IsNullOrEmpty(s)).ToArray();
string format = notNull.Length == 3 ? "{0} [{1}] ({2})"
: notNull.Length == 2 ? "{0} [{1}]" : "{0}";
string result = String.Format(format, notNull);
return result;
}
答案 3 :(得分:0)
public String GetName(string label, string dimension, string variable)
{
string result = String.Empty;
string labelFmt = !string.IsNullOrEmpty(label) ? string.Format("{0}", label) : string.Empty;
string dimenFmt = !string.IsNullOrEmpty(dimension) ? string.Format("[{0}]", dimension) : string.Empty;
string varFmt = !string.IsNullOrEmpty(variable) ? string.Format("({0})", variable) : string.Empty;
string firstSepStr = (!string.IsNullOrEmpty(label) &&
(!string.IsNullOrEmpty(variable) || !string.IsNullOrEmpty(dimension)))
? " "
: string.Empty;
string secondSepStr = (!string.IsNullOrEmpty(variable) && !string.IsNullOrEmpty(dimension))
? " "
: string.Empty;
return string.Format("{0}{1}{2}{3}{4}", labelFmt, firstSepStr, dimenFmt, secondSepStr, varFmt);
}
答案 4 :(得分:-1)
private String GetName(string variable, string label, string dimension)
{
StringBuilder result = new StringBuilder();
bool bEmpty = true;
if (!String.IsNullOrEmpty(label)
{
result.Append(label);
result.Append(' ');
}
if (!String.IsNullOrEmpty(dimension)
{
result.Append('[');
result.Append(dimension);
result.Append("] ");
}
if (!String.IsNullOrEmpty(variable)
{
result.Append('(');
result.Append(dimension);
result.Append(") ");
}
if (result.Count > 0)
{
result.Length := result.Length - 1;
}
return result.ToString();
}