C# PHP Explode Function

by Sam Allen - Updated January 8, 2010

You want to develop an equivalent to the PHP explode function in the C# programming language. In the PHP language, explode splits a string on another string, while the str_split separates characters. Here we see ways you can adapt your PHP code that uses the explode function to C# code that uses the Split method on the string type.

Using PHP explode

First, take a look at the PHP.net website's first example on explode. This PHP code will, when in a PHP file, print out the two pizza slices. The C# equivalent here uses the Split method with the string[] array overload. When you need to use a string[] array to split strings, you must provide the StringSplitOptions named constant.

~~~ Program that uses PHP explode ~~~

<?php
// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>

~~~ Program that uses Split instead of Explode (C#) ~~

using System;

class Program
{
    static void Main()
    {
        // Example 1
        string pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
        string[] pieces = pizza.Split(new string[] { " " },
            StringSplitOptions.None);
        Console.WriteLine(pieces[0]); // piece1
        Console.WriteLine(pieces[1]); // piece2
    }
}

~~~ Output of the program ~~~

piece1
piece2

Another PHP explode example

Now we look at the second example from PHP, which uses a style of coding developers don't normally use in the C# language. It assigns six strings to the string array returned by explode. In C# code, you cannot access past the end of the array returned by Split, which means you cannot assign strings to array positions that don't exist.

=== Program that uses PHP explode (PHP) ===

<?php
// Example 2
$data = "foo:*:1023:1000::/home/foo:/bin/sh";
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);
echo $user; // foo
echo $pass; // *
?>

=== Equivalent program in C# ===

using System;

class Program
{
    static void Main()
    {
        // Example 2
        string data = "foo:*:1023:1000::/home/foo:/bin/sh";
        string[] s = data.Split(new string[] { ":" }, StringSplitOptions.None);
        string user = null;
        string pass = null;
        string uid = null;
        string gecos = null;
        string home = null;
        string shell = null;

        if (s.Length > 0)
        {
            user = s[0];
        }
        if (s.Length > 1)
        {
            pass = s[1];
        }
        if (s.Length > 2)
        {
            uid = s[2];
        }
        if (s.Length > 3)
        {
            gecos = s[3];
        }
        if (s.Length > 4)
        {
            home = s[4];
        }
        if (s.Length > 5)
        {
            shell = s[5];
        }
        Console.WriteLine(user);
        Console.WriteLine(pass);
    }
}

=== Output of the C# program ===

foo
*

Explode method and limit

The PHP explode method with the limit parameter is not available in the .NET Framework, but a more complex method using Split with Join is possible. However, it would probably be easier to approach your problem a little differently, as with classes. This might also clarify your logic and error-checking.

str_split in PHP

I was interested to find that str_split doesn't split in the same way as does Split in Perl and C#. It splits based on character counts, not delimiters. To duplicate this functionality in C#, you could use ToCharArray() and a loop that counts characters. This would also give you more control, but would require more lines of code.

(Visit us3.php.net.)

Summary

We saw that many aspects of explode are readily available in the C# language. However, this article emphasizes the fundamental differences between scripting and compiled languages. The C# source here has more lines of code, and much stricter error checking and exactness requirements. PHP, like Perl, is less strict and therefore shorter.

(See Split String Examples.)

(Do not copy this page.)

Dot Net Perls | Search
Conversion | Convert Bool to Int | Convert Char Array to String | Convert List to Array | Convert String Array to String | List and String Conversion
C# | Obsolete Attribute | True and False | Gradient Tips | Catch Examples
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen