【问题标题】:best way of formatting string objects格式化字符串对象的最佳方式
【发布时间】:2012-07-09 00:42:40
【问题描述】:

我想创建一个这样的字符串对象

string data = "85-null-null-null-null-price-down-1-20";   // null if zero

我有这样的方法。

 public static DataSet LoadProducts(int CategoryId, string Size, 
                                   string Colour, Decimal LowerPrice, 
                                   Decimal HigherPrice, string SortExpression, 
                                   int PageNumber, int PageSize, 
                                   Boolean OnlyClearance)
 {
      /// Code goes here 
      /// i am goona pass that string to one more method here

      var result = ProductDataSource.Load(stringtoPass) // which accepts only the above format

 }

我知道我可以使用StringBuilder,但使用它需要太多的代码行。我在这里寻找一个简约的解决方案。

【问题讨论】:

  • 你说你想要一个字符串,然后你展示了一个返回类型为DataSet的方法,这是错字吗?
  • no.. 我正在处理方法中的那个字符串。
  • 为什么是 -1 的问题。有什么问题?

标签: c# string


【解决方案1】:

你可以这样做:

return string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}",
                     CategoryId,
                     Size ?? "null", 
                     Colour ?? "null",
                     LowerPrice != 0 ? LowerPrice.ToString() : "null",
                     HigherPrice != 0 ? HigherPrice.ToString() : "null",
                     SortExpression ?? "null",
                     PageNumber != 0 ? PageNumber.ToString() : "null",
                     PageSize != 0 ? PageSize.ToString() : "null", 
                     OnlyClearance);

为方便起见,您可以创建扩展方法:

public static string NullStringIfZero(this int value)
{
    return value != 0 ? value.ToString() : "null";
}

public static string NullStringIfZero(this decimal value)
{
    return value != 0 ? value.ToString() : "null";
}

并按如下方式使用它们:

return string.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}",
                     CategoryId,
                     Size ?? "null", 
                     Colour ?? "null",
                     LowerPrice.NullStringIfZero(),
                     HigherPrice.NullStringIfZero(),
                     SortExpression ?? "null",
                     PageNumber.NullStringIfZero(),
                     PageSize.NullStringIfZero(),
                     OnlyClearance);

【讨论】:

    【解决方案2】:
    string foo = String.Format("{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}-{8}",
                               CategoryId,
                               Size ?? "null" ... );
    

    【讨论】:

      【解决方案3】:

      请用您喜欢的格式覆盖对象的 ToString 方法并调用 Object.tostring() 方法

      评论后请求示例:

      public class Foo
      {
        public string Field1 {get; private set;}
        public string Field2 {get; private set;}
      
         public override string ToString()
         {
            return string.Format("Field1 = {0} , Field2 = {1}", Field1, Field2);
         }
      }
      

      现在这样做的好处是:

      1. 在您的方法中,您只能使用 1 个 Foo 类型的参数
      2. 当您在添加 foo 对象进行调试并在断点处停止时,您将看到字符串表示
      3. 如果您决定打印,您所要做的就是 Console.WriteLine(foo)

      【讨论】:

      • 为什么-2我说的和其他人一样,唯一的区别是我没有给出完整的代码
      • 请使用 ToString() 展示您的完整代码,我很想看看事情的不同之处...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 2010-10-25
      • 2015-12-18
      • 1970-01-01
      • 2011-01-29
      • 2015-12-02
      相关资源
      最近更新 更多