Looking for patterns and inserting elements

Using action code, what would be the easiest method to do the following:

  1. Add a “|” at the beginning and end of every line (the end defined as just before the “\n”);
  2. Immediately following the first line, add the following “|-------|--------|--------|\n”;

There are are other bits to the puzzle I’m working on, but but these re the two pieces where I’m stuck. Thanks.

I think I got it.

var:list vLine;
var:string vfirstLine;
var:string vText;


Can any of you think of a more optimal approach?

Optimal with respect to what?

Something like this might work:

var:boolean isFirst= true;
var:string result;

Not tested. This might be better if $Text were very long, since we don’t need to build the list of lines.

I’m reminded—since I had to go check the String.eachLine() was added very recently, in v9.6.0. The main likely use is, as here, with $Text.

1 Like

Optimal in terms of efficiency, fewer lines of code.

In this context, is the if(isFirst) testing if this is the first line? I’m not exactly sure how this shorthand works, but I like the approach. Thanks.


My variant ought to be a little bit faster, since it scans through the text once, while the original scans the text twice. And it uses a little less memory, since the original builds a second copy of the text, as a list. Neither is likely to be consequential unless $Text is enormous.

1 Like