How to split multi-line string at newline?

My sample test code generates the following output with three files selected. Evidently the split is not working using "\n" = newline. What would be the correct split term to use?

var c = DOpus.Create.command; c.runcommand("Clipboard COPYNAMES"); var s = DOpus.GetClip; DOpus.output(s); var a = s.split("\\n"); DOpus.output(a.length);
C:\setup.log
C:\AdobeDebug.txt
C:\servers.ini
1 (expected to be 3)

Regards, AB

You need \n instead of \n - the double-backslash escapes the \ and so you are literally asking it to split on the string "\n" instead of a cr/lf.

Thanks. As ever, so simple once pointed out... :blush:

Regards, AB

And for what it's worth, the split statement needs to be var a = s.split("\r\n"); in order to split at CRLF.

Regards, AB

Might be worth testing the newline chars first, before splitting file/text/clipboard content line by line.
You never know the flavour of line ending the text to split comes in, might be amiga or linux and whoops, script will fail. o)

var GetLineEndingOfText = function(text){ var lineEndings = ["\r\n","\n\r","\n","\r"], lineEnding = ""; for(var l=0;l<lineEndings.length;l++){ if (text.indexOf(lineEndings[l])!=-1){ lineEnding = lineEndings[l]; break; } } return lineEnding; }

split supports regular expressions so you can use this to split:

lineEndings=text.split(/(?:\n|\r\n?)/);

The line above allows for all common lineendings, none of which will be
present in the resulting array.

Yes, even better! o)

Thanks gents. :thumbsup:

Regards, AB