views:

162

answers:

3
+1  A: 

You should use a StringBuilder rather than just string aggregation, especially if this is going to be used a lot.

You can also use String.Join() to put a delimited string array back together.

ck
http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html
Rubys
+2  A: 

Yes that seems to be ok.

About StringBuilder:
No need to use StringBuilder unless there are usually more than 4-5 elements after the split. If there are usually less than that then aggregation is fine.

Sani Huttunen
Depends how many times this function is called... If it's caled within a loop then there could be far more than that..
ck
+7  A: 

How about:

String.Join(" « ", "Products » X1 » X3".Split(new[]{" » "}, 
    StringSplitOptions.None).Reverse().ToArray());

EDIT: The updated version version will work if the components contain spaces (e.g. "Foo Products » X1 » X3")

Matthew Flaschen
Wow, very short and single line, like it... But I'm having error: "No overload for method 'Reverse' takes 0 arguments"
HasanGursoy
@Hasan, you need to add `using System.Linq;` to get [`Enumerable.Reverse`](http://msdn.microsoft.com/en-us/library/bb358497.aspx).
Matthew Flaschen