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.
 
The logic of the replication method is straight forward, no need to dig into that.  The reason of the post is the extension method, so a quick review of what it takes to make one.
 
  1. A Static Class
  2. A Static Method
  3. 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 :)
 
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Core
  5. {
  6.   /// <summary>
  7.   /// Where should replicated input
  8.   /// </summary>
  9.   public enum ReplicationMode
  10.   {
  11.     /// <summary>
  12.     /// Replicated input is added behind the source
  13.     /// </summary>
  14.     Append,
  15.     /// <summary>
  16.     /// Replicated input is added in front of the source
  17.     /// </summary>
  18.     Prepend
  19.   }
  20.  
  21.   /// <summary>
  22.   /// Extension methods
  23.   /// </summary>
  24.   public static class Extensions
  25.   {
  26.     /// <summary>
  27.     /// Replicates the input string the specified number of times in front of or behind the source string
  28.     /// </summary>
  29.     /// <param name="source">The string that the replicated input is being added to.</param>
  30.     /// <param name="input">The string to replicate.</param>
  31.     /// <param name="count">The number of times to replicate the input.</param>
  32.     /// <param name="replicationMode">The positioning of the replicated data.</param>
  33.     /// <returns>The source string if the input is null, empty or the count is less than one.  Otherwise the processed result of replication.</returns>
  34.     public static string Replicate(this string source, string input, int count, ReplicationMode replicationMode)
  35.     {
  36.       // validate input
  37.       if (string.IsNullOrEmpty(input) == true) return source;
  38.       if (count < 1) return source;
  39.  
  40.       // Prime the result
  41.       StringBuilder Result = new StringBuilder(source.Length + (input.Length * count));
  42.       Result.Append(source);
  43.  
  44.       for (int i = 0; i < count; i++)
  45.       {
  46.         if (replicationMode == ReplicationMode.Append)
  47.           Result.Append(input);
  48.         else
  49.           Result.Insert(0, input);
  50.       }
  51.  
  52.       return Result.ToString();
  53.     }
  54.   }
  55. }

No Comments