Handy Web Path Concatenation Code
Lot of web developement code is spent concatenating path snippets and I always hated having to deal with slashes. I wanted a function that was consistent with its return type, and very loose in its parameter requirements. I ended up with the following:
Test First:
using bddunit.core; using CBC.Radio3.Commons.Specs; using Observation = MbUnit.Framework.TestAttribute; using SUT = CBC.Radio3.Commons.Web.WebPath; namespace CBC.Radio3.Commons.Web { [Concern(typeof (WebPath))] public class when_WebPath_Combines_a_bunch_of_strings : ContextSpecification { private string result; protected override void establish_context() { } protected override void because() { result = SUT.Combine("/slashesonbothsides/", "//doubleslash//", "rightslash/", "/leftslash", "file.jpg"); } [Observation] public void should_return_an_absolute_web_path() { result.should_be_equal_to("/slashesonbothsides/doubleslash/rightslash/leftslash/file.jpg"); } } }
The BDD style of testing is inspired (and pretty much copied) from JP Boodhoo's Nothing but .Net class.
Here's the code:
using System.Text; namespace CBC.Radio3.Commons.Web { public class WebPath { /// <summary> /// Takes a parameter list of strings and returns the strings concatenated with a /// preceding '/' character. If the string has a trailing/preceding slash it will /// be removed. Any slashes in the middle of the string will remain. /// </summary> /// <param name="paths"></param> /// <returns>Contatenation of paths seperated by directory seperator</returns> public static string Combine(params string[] paths) { var sb = new StringBuilder(); foreach (var path in paths) sb.AppendFormat("/{0}", path.TrimEnd('/').TrimStart('/')); return sb.ToString(); } } }
That was my first go at it. I have a feeling I might generalize the class to be a delimited string builder. Which would be nice because then the method won't be static. Anyways, just thought I would share something that I think is concise but will be used a lot.
Similar Posts
- Unit Testing Domain Persistence With NDbUnit, NHibernate and SQLite
- When 1 Does Not Equal 1, A Debugging Tale
- It's Good To Laugh At Your Old Code

Comments
Ted Jardine on on 12.05.2008 at 8:51 PM
Hey Scott! Came across your comment on Scott Hanselman's blog @ www.hanselman.com/.../DellMini9Practi and thought I'd drop by and say hi. Brought a smile seeing you JP-stylin'...oh so familiar now, although I've only just now been able to start doing more tests as I've been so behind the eight ball with projects for awhile.
I bought an EEE PC 1000H and would highly recommend it (especially over the Dell). This or the Samsung. Love it. Bought it for the same reasons you mentioned wanting it for. I'm actually quite shocked, as performance-wise, it's actually much better than I anticipated. In fact, it's better than the laptop I used for JP's course which was my main development machine only two years ago. Popped in a total of 2GB RAM.... If you don't need bluetooth and Wireless-N, can pick up the 1000HA for$70 cheaper.
Pros:
- The screen is massively bright. Really really bright.
- The mouse gestures on the 1000H are the best I've seen on a PC. Not as good as a Mac, but still good (two finger scroll, three finger right-click).
- Four hours of battery life under heavy usage! Even more under less usage.
- Can't wait to drop Windows 7 on it.
- No heat whatsoever.
Cons
- Wish it had a bigger screen.
- Good thing mouse gestures rock, because the mouse buttons suck: take a lot of pressure to click. Although at least they're in the right position compared to a lot of netbooks
- XP Home. I'll get around to putting XP Pro on, but even then...apparently Vista runs fine, but...
- the shift key is in the wrong spot, there was a Fn lock for when I read the newspaper on it I could easily page down/up with just one button instead of two
Ciao.
software developer on on 8.17.2009 at 5:10 AM
Birilliant Idea I had the same problem but was never able to salve it, Thanks for sharing that piece of coode , that would really help