Alternative of php's explode/implode-functions in c#

夙愿已清 提交于 2019-11-29 15:55:01

问题


are there a similar functions to explode/implode in the .net-framework?

or do i have to code it by myself?


回答1:


String.Split() will explode, and String.Join() will implode.




回答2:


The current answers are not fully correct, and here is why:

all works fine if you have a variable of type string[], but in PHP, you can also have KeyValue arrays, let's assume this one:

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

and now call the implode method as echo implode("", $params); your output is

12345678910095DKK

and, let's do the same in C#:

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

and use String.Join("", kv) we will get

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

not exactly the same, right?

what you need to use, and keep in mind that's what PHP does, is to use only the values of the collection, like:

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

and then, yes, it will be the same as the PHP implode method

12345678910095DKK

You can test PHP code online using http://WriteCodeOnline.com/php/




回答3:


There are two methods that correspond to PHP's explode and implode methods.

The PHP explode's equivalent is String.Split. The PHP implode's equivalent is String.Join.



来源:https://stackoverflow.com/questions/7598826/alternative-of-phps-explode-implode-functions-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!