C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
It takes an input string and divides it into sections of a specific size. These are stored together in an array. There is currently no built-in method you can directly call.
Example. This program contains an extension method with name Explode and the Main entry point method. The extension method is inside a static class. The extension method Explode receives two parameters.
Parameters: We pass the string instance as the first parameter. And the second indicates the number of characters each section should have.
Internally, the Explode method implements some logic for separating a string into split parts, each a certain length except the last which may be shorter. This is explained after the sample.
C# program that explodes strings with limit using System; static class Extensions { public static string[] Explode(this string value, int size) { // Number of segments exploded to except last. int count = value.Length / size; // Determine if we need to store a final segment. // ... Sometimes we have a partial segment. bool final = false; if ((size * count) < value.Length) { final = true; } // Allocate the array to return. // ... The size varies depending on if there is a final fragment. string[] result; if (final) { result = new string[count + 1]; } else { result = new string[count]; } // Loop through each index and take a substring. // ... The starting index is computed with multiplication. for (int i = 0; i < count; i++) { result[i] = value.Substring((i * size), size); } // Sometimes we need to set the final string fragment. if (final) { result[result.Length - 1] = value.Substring(count * size); } return result; } } class Program { static void Main() { const string input = "carrot"; string[] explode = input.Explode(2); Console.WriteLine(string.Join(",", explode)); explode = input.Explode(2); Console.WriteLine(string.Join(",", explode)); explode = input.Explode(3); Console.WriteLine(string.Join(",", explode)); explode = input.Explode(4); Console.WriteLine(string.Join(",", explode)); explode = input.Explode(100); Console.WriteLine(string.Join(",", explode)); } } Output ca,rr,ot ca,rr,ot car,rot carr,ot carrot
The Explode extension method uses the this-modifier on its first parameter. This is the syntax for an extension method instance variable. The second parameter is the size of the segments you want to split to.
The internal logic first estimates the size of the result array—it adds one if the division was rounded down. Then, it allocates the array. Next it loops through the string and assigns Substring results using computed indices.
Finally: It appends the final string part to the exploded array. It assigns to the last index of the array.
PHP. In PHP, explode splits a string on another string. The str_split separates characters. We adapt PHP code that uses the explode function to C# code that uses the Split method on the string type.
First, 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.
Tip: When you need to use a string array to split strings, you must provide the StringSplitOptions named constant.
Program 1: PHP <?php // Example 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 ?> C# program that uses Split instead of Explode 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 piece1 piece2
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.
Tip: In C# code, you cannot access past the end of an array. You cannot assign strings to array positions that don't exist.
Program 2: 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: 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 foo *
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 be easier to approach your problem differently, as with classes.
Summary. We implemented an Explode extension method. We used a loop with Substring method calls, and this reduces allocations and results in good performance. The method uses division to estimate the result array size.
And: We demonstrated the correctness of the method. We pointed out its differences to Split on the string type.