What is common way to split string into list with CMAKE?

后端 未结 3 375
轻奢々
轻奢々 2020-12-13 16:59

Imagine I have the following string :

set(SEXY_STRING \"I love CMake\")

then I want to obtain SEXY_LIST from SEXY_STRING

相关标签:
3条回答
  • 2020-12-13 17:21

    Replace your separator by a ;. I don't see any other way to do it.

    cmake_minimum_required(VERSION 2.8)
    
    set(SEXY_STRING "I love CMake")
    string(REPLACE " " ";" SEXY_LIST ${SEXY_STRING})
    
    message(STATUS "string = ${SEXY_STRING}")
    # string = I love CMake
    
    message(STATUS "list = ${SEXY_LIST}")
    # list = I;love;CMake
    
    list(LENGTH SEXY_LIST len)
    message(STATUS "len = ${len}")
    # len = 3
    
    0 讨论(0)
  • 2020-12-13 17:28

    You can use the separate_arguments command.

    cmake_minimum_required(VERSION 2.6)
    
    set(SEXY_STRING "I love CMake")
    
    message(STATUS "string = ${SEXY_STRING}")
    # string = I love CMake
    
    set( SEXY_LIST ${SEXY_STRING} )
    separate_arguments(SEXY_LIST)
    
    message(STATUS "list = ${SEXY_LIST}")
    # list = I;love;CMake
    
    list(LENGTH SEXY_LIST len)
    message(STATUS "len = ${len}")
    # len = 3
    
    0 讨论(0)
  • 2020-12-13 17:37
    string(REGEX MATCHALL "[a-zA-Z]+\ |[a-zA-Z]+$" SEXY_LIST "${SEXY_STRING}")
    
    0 讨论(0)
提交回复
热议问题