C# String Replication Extension Method
I finally got around to installing Visual Studio 2008 tonight and wanted to write a quick piece of code using a new feature. The first thing that came to mind was a string replication extension method. This was inspired by something I just wrote in 2.0 that at the time made me think "this would be a good extension method." I realize the code I have isn't breaking any ground, but I thought I'd share it anyway.
Right now, it seems like the usefulness of extension methods and their pitfalls is being hashed out.
A couple examples:
Perhaps I'm caught up in the shiny new language feature and I'm not seeing the downside. I do know this, having to remember things like "where is that SQL string cleaning method?", "don't we have something that strips HTML out of a string?", "where is that email address validator?", etc can be a pain and cause unnecessary duplication of effort. I love the thought of being to bolt my string methods to the string object. In doing so, I'm centralizing my string manipulation methods.
Something to note here is the notion of extension methods being implemented in their own namespace. It could start getting ugly if extension methods showed up without invitation. For example, if someone references your assembly and then adds a using statement, they might not be expecting to pick up all of your extension methods from that assembly. Placing extension methods into their own namespace would help prevent this from happening.
The Task & Code
Create a method that will take a source string and an input string, and append/prepend the input string a specified number of times to the source string.
-
A Static Class
-
A Static Method
-
The first parameter of the method having the this keyword, indicating which type the method will be bound to.
From here, the rest is academic...
Usage of the code below might look like this:
string Foo = "Foo".Replicate("Bar", 5, ReplicationMode.Append);
Not exactly the most exciting example. At some point I would like to modify this to act like String.PadLeft or String.PadRight, but since it is getting late, I'll go with what I have for now...
Enjoy :)
-
using System;
-
using System.Text;
-
-
namespace Core
-
{
-
/// <summary>
-
/// Where should replicated input
-
/// </summary>
-
public enum ReplicationMode
-
{
-
/// <summary>
-
/// Replicated input is added behind the source
-
/// </summary>
-
Append,
-
/// <summary>
-
/// Replicated input is added in front of the source
-
/// </summary>
-
Prepend
-
}
-
-
/// <summary>
-
/// Extension methods
-
/// </summary>
-
public static class Extensions
-
{
-
/// <summary>
-
/// Replicates the input string the specified number of times in front of or behind the source string
-
/// </summary>
-
/// <param name="source">The string that the replicated input is being added to.</param>
-
/// <param name="input">The string to replicate.</param>
-
/// <param name="count">The number of times to replicate the input.</param>
-
/// <param name="replicationMode">The positioning of the replicated data.</param>
-
/// <returns>The source string if the input is null, empty or the count is less than one. Otherwise the processed result of replication.</returns>
-
public static string Replicate(this string source, string input, int count, ReplicationMode replicationMode)
-
{
-
// validate input
-
if (string.IsNullOrEmpty(input) == true) return source;
-
if (count < 1) return source;
-
-
// Prime the result
-
Result.Append(source);
-
-
for (int i = 0; i < count; i++)
-
{
-
if (replicationMode == ReplicationMode.Append)
-
Result.Append(input);
-
else
-
Result.Insert(0, input);
-
}
-
-
return Result.ToString();
-
}
-
}
-
}