Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate/join list of numbers to get a string

Tags:

j

I want to join a list of integers in their order to obtain a final string, e.g.

a=.11 22 33 44

And I want to get '11223344'. And also, sometimes, I need to put a separator in between '11 22 33 44' like a space in this case. What is the simplest way?


2 Answers

Use format ": to covnert the list of numbers to a string:

":a
11 22 33 44

There are many ways to remove the space between them or to separate them with another string.

' ' -.~ ":a          NB. remove spaces (' ') from ":a
11223344

(":a) rplc ' ';', '  NB. replace spaces with ', ' (or anything else, even empty)
11, 22, 33, 44

,('-',~":)"0 a       NB. append '-' to every element of a; convert to string
11-22-33-44-

etc

like image 99
Eelvex Avatar answered Feb 04 '26 01:02

Eelvex


Another solution that can make it easier to deal with some situations.

Use the foreign 8!:0 to convert the numbers to boxed strings (this handles conversion of negative signs from _ to -).

   8!:0 a
┌──┬──┬──┬──┐
│11│22│33│44│
└──┴──┴──┴──┘

Use the joinstring verb from the strings script/addon to join the boxed strings as required. For versions prior to J7 you will need to require 'strings' to ensure that the joinstring verb is available.

   ' ' joinstring 8!:0 a
11 22 33 44
   '' joinstring 8!:0 a
11223344
   '","' joinstring 8!:0 a
11","22","33","44
like image 20
Tikkanz Avatar answered Feb 03 '26 23:02

Tikkanz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!