【问题标题】:Alternative of php's explode/implode-functions in c#在c#中替代php的explode/implode-functions
【发布时间】:2011-11-27 18:27:00
【问题描述】:

.net-framework 中是否有类似的爆炸/内爆功能?

还是我必须自己编码?

【问题讨论】:

    标签: c# php explode implode


    【解决方案1】:

    有两种方法对应PHP的explode和implode方法。

    PHP 爆炸的等价物是String.Split。 PHP 内爆的等价物是String.Join

    【讨论】:

      【解决方案2】:

      当前答案并不完全正确,原因如下:

      如果你有一个string[] 类型的变量,一切都很好,但是在PHP 中,你也可以有KeyValue 数组,我们假设这个:

      $params = array(
          'merchantnumber' => "123456789", 
          'amount' => "10095", 
          'currency' => "DKK"
      );
      

      现在调用implode 方法为echo implode("", $params); 你的输出是

      12345678910095DKK
      

      并且,让我们在 C# 中做同样的事情:

      var kv = new Dictionary<string, string>() {
                   { "merchantnumber", "123456789" },
                   { "amount", "10095" },
                   { "currency", "DKK" }
               };
      

      并使用String.Join("", kv),我们将得到

      [merchantnumber, 123456789][amount, 10095][currency, DKK]
      

      不完全一样,对吧?

      您需要使用,并记住 PHP 所做的就是仅使用集合的值,例如:

      String.Join("", kv.Values);
      

      然后,是的,它将与 PHP implode 方法相同

      12345678910095DKK
      

      您可以使用http://WriteCodeOnline.com/php/在线测试 PHP 代码

      【讨论】:

      • 公平地说,加入字典的值并不是一个常见的用例。
      • @Brilliand 是在与支付提供商及其安全性打交道时。
      【解决方案3】:

      String.Split() 会爆炸,String.Join() 会内爆。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-03
        • 1970-01-01
        • 1970-01-01
        • 2023-02-01
        相关资源
        最近更新 更多