0

I have MyCustomMacro that returns me list of strings. I need to show this list with custom separator beetwen list elements.

{% 
foreach (ev in MyCustomMacro("events") )
{ 
  ev + " | "; 
};
#%}

but this code also addes " | " after last element.

How can i check that element is last in the list?

  • 1
    Possible duplicate of [How do you get the index of the current iteration of a foreach loop?](http://stackoverflow.com/questions/43021/how-do-you-get-the-index-of-the-current-iteration-of-a-foreach-loop) – Bryan Mar 22 '16 at 13:02

4 Answers4

3

if you can get the values in a string array, you can do:

string.Join("|", events[])

Examples

Zach Perry
  • 736
  • 3
  • 12
2

I think a much efficient way is to use string StringBuilder class.

Something on these lines.

{%
  var builder = new StringBuilder();
  foreach (ev in MyCustomMacro("events") )
  { 
    builder.append(ev + " | "); 
  };
  result = String.Join("|", builder.Split('|'));
%}
Chetan Sharma
  • 1,260
  • 11
  • 13
0

You can do it easly by this:

{% result="";
   foreach (ev in MyCustomMacro("events") )
    { 
     result+= ev + " | "; 
    };
   result.TrimEnd(" | ")
%}
Dawid Jachnik
  • 447
  • 2
  • 4
-1

You can use join method of string class:

{% string.join("|", MyCustomMacro("events")) %}