Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

or do i have to code it by myself?

like image 333
Sheldon Cooper Avatar asked Sep 29 '11 14:09

Sheldon Cooper


People also ask

What is the difference between implode () and explode () functions?

PHP Explode function breaks a string into an array. PHP Implode function returns a string from an array. PHP implode and PHP explode are two common functions used in PHP when working with arrays and strings in PHP.

What would implode explode string )) do?

As the name suggests Implode/implode() method joins array elements with a string segment that works as a glue and similarly Explode/explode() method does the exact opposite i.e. given a string and a delimiter it creates an array of strings separating with the help of the delimiter.

Which one is syntax for explode function?

PHP explode() Functionprint_r (explode(" ",$str));

What is the use of implode () function?

The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.


2 Answers

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

like image 138
scottm Avatar answered Sep 28 '22 02:09

scottm


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/

like image 36
balexandre Avatar answered Sep 28 '22 02:09

balexandre