Line Separation for files from Windows, Linux and MacOS - Forum

Forum Navigation
You need to log in to create posts and topics.

Line Separation for files from Windows, Linux and MacOS

In response to this post ... https://visualneo.com/forum/topic/help-to-arrays-ayuda-para-usar-arrays ... @roccocogliano wrote ...

Unix/Linux file systems use newlines (\n).
MacOS uses carriage-returns (\r).
Windows uses a carriage-return followed by a newline (\r\n).
So first check the line terminator of your file.

One way to avoid checking for the terminator is to have a string (text) that will parse no matter what the terminator character(s) was (were) ...

... in order to test out this functionality with all 3 "new-line-codes", let us make a string with all different combinations
SetVar "[MultiLineText]" "abcd\r\n1234\rqwerty\nzxcv"
...AlertBox "MultiLinetext" "[MultiLineText]" ""

... replace all instances of \r\n to \r (will not affect strings with \r OR \n)
StrReplace "[MultiLineText]" "\r\n" "\r" [temp1] ""
... now you are left with the possibility of strings having \r OR \n
... replace all instances of \r to \n (will not affect strings with \n)
StrReplace "[temp1]" "\r" "\n" [temp2] ""
... and finally, all strings have \n as their terminator; so just go ahead and StrParse on \n
StrParse "[temp2]" "\n" [lineNumber]

... verify it
SetVar "[AlertText]" "[MultiLineText]<br/>line 1 = [lineNumber(0)]<br/>line 2 = [lineNumber(1)]"
Setvar "[AlertText]" "[AlertText]<br/>line 3 = [lineNumber(2)]<br/>line 4 = [lineNumber(3)]"
AlertBox "lines" "[AlertText]" ""

If you have to parse many such strings in your App, you can turn this into a subroutine .. or even a plugin that can then be used in all your Apps.

luishp and roccocogliano have reacted to this post.
luishproccocogliano

Great!