You want to explode a string in your C# program, taking an input string and dividing it into sections of a specific size, which are stored together in an array. There is currently no method you can directly call, but you can implement an efficient Explode method using the extension method syntax form. Here, we implement an explode method and then describe and test it in a console program.
First, this program text contains an extension method with name Explode and also the Main entry point method. The extension method is inside a static class. The extension method Explode receives two parameters, with the first being the instance parameter and the second being the size parameter which indicates the number of characters each section of the result 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.
--- Program that explodes strings with limit (C#) ---
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 of the program ---
ca,rr,ot
ca,rr,ot
car,rot
carr,ot
carrotDescription of extension method. 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, and then adds one if the division was rounded down. Then, it allocates the array with the exact size. Next, it loops through the string and assigns Substring results using computed indices. Finally, it appends the final string part to the exploded array.
In this part, we discuss the Split method and how it relates to this Explode extension method. The Split method cannot split based on substring sizes; the Explode method can only split based on substring sizes. In some languages such as PHP, the Explode method does both of these tasks. The Split method is built into the string type in the .NET 3.5 Framework; this Explode method must be added manually to each solution.
In this article, we implemented an Explode extension method to fill in functionality from other languages into the C# language. 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. We demonstrated the correctness of the method as well as pointed out its differences to Split on the string type.