Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / pas2js / 2019-10

How to split a string using an array as separator?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hi.

In pure JS, we can split a string using an array as separator (please see "Splitting with an array as separator" here
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>), e.g.:

var s = 's:foo.bar';
var a = s.split(/[:\.]/);
console.log(a);
(3) ["s", "foo", "bar"]

So, is there any trick to do something like this in pure Pascal? I tried:

A := TJSString(S).split([':', '.']); // error at transpile time
A := TJSString(S).split(':', '.'); // error at transpile time
A := TJSString(S).split('/[:.]/'); // the JS got only a static string

Since the TJSString.split() signature allows only a static string as first parameter, calls like those above don't work at all, then as workaround I solved it using an asm block, however, it would be nice to allow pure Pascal in that.

Thanks in advance for any help!

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20191004/5cebaf4a/attachment.html>
Michael Van Canneyt
to silvioprog
silvioprog wrote 23 lines
Hi. In pure JS, we can split a string using an array as separator (please see "Splitting with an array as separator" here <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split>), e.g.: var s = 's:foo.bar'; var a = s.split(/[:\.]/); console.log(a); (3) ["s", "foo", "bar"] So, is there any trick to do something like this in pure Pascal? I tried: A := TJSString(S).split([':', '.']); // error at transpile time A := TJSString(S).split(':', '.'); // error at transpile time A := TJSString(S).split('/[:.]/'); // the JS got only a static string Since the TJSString.split() signature allows only a static string as first parameter, calls like those above don't work at all, then as workaround I solved it using an asm block, however, it would be nice to allow pure Pascal in that.

Hm.
The documentation text does not mention the possibility of an array, so I didn't add that. I have added an overload.

Michael.

silvioprog
to Michael Van Canneyt
PM Michael Van Canneyt wrote 5 lines
Hm. The documentation text does not mention the possibility of an array, so I didn't add that. I have added an overload. Michael.

Indeed, sometimes the MDN web docs lacks information on top and are present only on bottom (examples area).

Thank you for solving! (y)

Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20191007/f24fd70b/attachment.html>
silvioprog
to silvioprog

Oops, I forgot to send a patch. ^^'

Could you apply this one in attachment? The String.prototype.split() (and many other string functions) allows to pass as *separator*(*) the most powerful feature also presente in JS: regular expressions. After applying the patch, it allows:

uses JS, Web;
var
  S: string;
  A: TStringDynArray;
begin
  S := 's:foo.bar';
  A := TJSString(S).split(TJSRegexp.New('[:.]'));
  console.log(A);
end.

Result:

(3) ["s", "foo", "bar"]

The equivalent version in pure JS (notice the regex part as parameter):

var s = 's:foo.bar';
var a = s.split(/[:\.]/);
console.log(a);

Result:

(3) ["s", "foo", "bar"]

Thank you again!

(*) Snip from MDN
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split> :

*"... Separator*
*Specifies the string which denotes the points at which each split should occur. The separator is treated as a string or as a regular expression."*

AM silvioprog wrote 18 lines
On Fri, Oct 4, 2019 at 6:33 PM Michael Van Canneyt wrote: > Hm. > The documentation text does not mention the possibility of an array, so I > didn't add that. I have added an overload. > > Michael. Indeed, sometimes the MDN web docs lacks information on top and are present only on bottom (examples area). Thank you for solving! (y) -- Silvio Clécio
Silvio Clécio -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20191007/f87f67af/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: js.pas.diff Type: text/x-patch Size: 856 bytes Desc: not available URL: <http://lists.freepascal.org/pipermail/pas2js/attachments/20191007/f87f67af/attachment.bin>