Skip to main content

StringUtils

#include <string_utils.hpp>

Public Static Methods

ReturnNameDescription
size_tsplit staticSplit string by delimiter with optional trimming.
size_tsplit staticSplit string by single character delimiter (faster)
std::stringtrim staticTrim whitespace from both ends.
std::stringltrim staticTrim whitespace from left.
std::stringrtrim staticTrim whitespace from right.
std::stringjoin staticJoin strings with delimiter.
std::stringto_lower staticConvert string to lowercase.
std::stringto_upper staticConvert string to uppercase.
boolstarts_with staticCheck if string starts with prefix.
boolends_with staticCheck if string ends with suffix.
boolcontains staticCheck if string contains substring.
std::stringreplace_all staticReplace all occurrences of ‘from’ with ‘to’.
voidsplit_respecting_quotes staticSplit keeping quoted sections intact.
std::stringremove_whitespace staticRemove all whitespace from string.
std::stringpad_left staticPad string to width with fill character.
std::stringpad_right static
std::stringescape_json staticEscape special characters for JSON.

split

static
static inline size_t split(const std::string & s, std::vector< std::string > & result, const std::string & delim, bool keep_empty, bool trim_space)
Split string by delimiter with optional trimming.

Parameters

  • s Input string
  • result Output vector (appends to existing elements)
  • delim Delimiter string
  • keep_empty Keep empty tokens
  • trim_space Trim whitespace from each token

Returns

Number of characters processed Example:
std::vector<std::string> parts;
[StringUtils::split](#split)("drone1,drone2, drone3", parts, ",");
// parts = {"drone1", "drone2", "drone3"}

split

static
static inline size_t split(const std::string & s, std::vector< std::string > & result, char delim, bool keep_empty, bool trim_space)
Split string by single character delimiter (faster)

trim

static
static inline std::string trim(const std::string & s)
Trim whitespace from both ends.

ltrim

static
static inline std::string ltrim(const std::string & s)
Trim whitespace from left.

rtrim

static
static inline std::string rtrim(const std::string & s)
Trim whitespace from right.

join

static
static inline std::string join(const std::vector< std::string > & parts, const std::string & delimiter)
Join strings with delimiter. Example:
std::vector<std::string> ids = {"DRONE-1", "DRONE-2", "DRONE-3"};
std::string csv = [StringUtils::join](#join)(ids, ", ");
// csv = "DRONE-1, DRONE-2, DRONE-3"

to_lower

static
static inline std::string to_lower(const std::string & s)
Convert string to lowercase.

to_upper

static
static inline std::string to_upper(const std::string & s)
Convert string to uppercase.

starts_with

static
static inline bool starts_with(std::string_view s, std::string_view prefix)
Check if string starts with prefix.

ends_with

static
static inline bool ends_with(std::string_view s, std::string_view suffix)
Check if string ends with suffix.

contains

static
static inline bool contains(std::string_view s, std::string_view substr)
Check if string contains substring.

replace_all

static
static inline std::string replace_all(std::string s, const std::string & from, const std::string & to)
Replace all occurrences of ‘from’ with ‘to’.

split_respecting_quotes

static
static inline void split_respecting_quotes(const std::string & s, std::vector< std::string > & result, char delimiter, char quote_char)
Split keeping quoted sections intact. Example:
std::vector<std::string> parts;
[StringUtils::split_respecting_quotes](#split_respecting_quotes)("field1,\"value,with,commas\",field3", parts, ',', '"');
// parts = {"field1", "value,with,commas", "field3"}

remove_whitespace

static
static inline std::string remove_whitespace(const std::string & s)
Remove all whitespace from string.

pad_left

static
static inline std::string pad_left(const std::string & s, size_t width, char fill)
Pad string to width with fill character.

pad_right

static
static inline std::string pad_right(const std::string & s, size_t width, char fill)

escape_json

static
static inline std::string escape_json(const std::string & s)
Escape special characters for JSON.