Question

How do I convert a list of strings into a string in DrRacket? For example, if I have '("44" "444") convert it into "44 444"?

I tried string-join, but it takes a delimiter and if I put one it replaces the space with the delimiter and if I use "" for the delimiter it simply gets rid of it.

Was it helpful?

Solution

In fact string-join is the right procedure for using in this case, simply use " " (a single space) as delimiter:

(string-join '("44" "444") " ")
=> "44 444" 

Just to clarify: in a list the spaces between elements are not considered part of the list, they're there to separate the elements. For example, all these lists are equal and evaluate to the same value:

'("44""444")
'("44" "444")
'("44"  "444")

If for some reason you want to consider the spaces as part of the list then you have to explicitly add them as elements in the list:

(define lst '("a" " " "b" "  " "c" "   " "d"))
(string-join lst "")
=> "a b  c   d"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top