The string() Command
Explore the CMake string command modes to manipulate strings, perform search and replace, comparisons, hashing, generation, and JSON operations. Understand how to apply these functions to automate and optimize your C++ build process using practical examples.
We'll cover the following...
The string() command is used to manipulate strings. It comes with a variety of modes that perform different actions on the string:
Search and replace
Manipulation
Comparison
Hashing
Generation
JSON operations (the last one available since CMake 3.19)
Full details can be found in the online documentation.
string() modes that accept the <input> argument will accept multiple <input> values and concatenate them before the execution of the command:
string(PREPEND myVariable "a" "b" "c")
This is the equivalent of the following:
string(PREPEND myVariable "abc")
Let's explore all available string() modes.
Search and replace
The following modes are available:
string(FIND <haystack> <pattern> <out> [REVERSE])searches for<pattern>in the<haystack>string and writes the position found as an integer to the<out>variable. If theREVERSEflag was used, it searches from the end of the string to the beginning. This works only for ASCII strings (multibyte support isn't provided).string(REPLACE <pattern> <replace> <out> <input>)replaces all occurrences of<pattern>in<input>with<replace>and stores them in the<out>variable.string(REGEX MATCH <pattern> <out> <input>)regex-matches the first occurrence of<pattern>in<input>with<replace>and stores it in the<out>variable.string(REGEX MATCHALL <pattern>...