15

I am confused with a very simple example. I have a standard list, so basically its string representation uses semicolon as delimiters. I want to replace it by another one:

set(L1 "A" "B" "C")
message("L1: ${L1}")

string(REPLACE ";" "<->" L2 ${L1})
message("L2: ${L2}")

this snippet prints:

L1: A;B;C
L2: ABC

and I don't understand why. According to some other SO answers, my string replacement seems valid. What am I doing wrong ? Is there a way to store the value A<->B<->C in my 2nd variable ?

Note: I use CMake 3.7.2

Community
  • 1
  • 1
Antwane
  • 20,760
  • 7
  • 51
  • 84

2 Answers2

21

Just put ${L1} in quotes:

set(L1 "A" "B" "C")
message("L1: ${L1}")

string(REPLACE ";" "<->" L2 "${L1}")
message("L2: ${L2}")

Otherwise the list will be expanded again to a space separated parameter list.

Reference

Florian
  • 39,996
  • 9
  • 133
  • 149
9

But ${L1} isn't a string, it's a list. If you want a string then you need to enclose it in double-quotes like "${L1}".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621