I am doing string parsing and essentially what I would like to do is something like this:
string signature = char[index+1] + '/' + char[index+2];
BUT you can't do string concatenation on char's so that brings me to this question, how can I simulate concatenation on char's?
I know that the string library in C++ has append but I don't think that works for my case. Any ideas?
The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.
1. Using to_string() function. The most commonly used approach to concatenate an integer to a string object in C++ is to call the std::to_string function, which can return the string representation of the specified integer.
Concatenate Two String ArraysConcatenate text with the strcat function. Note that when concatenated in this way the output string will insert a whitespace character between the input strings. Strings and character vectors can be combined using strcat .
Concatenating strings would only require a + between the strings, but concatenating chars using + will change the value of the char into ascii and hence giving a numerical output.
You can concatenate chars to a std::string
, you just need one of the operands to be a std::string
, otherwise you are adding integers.
std::string signature = std::string() + char_array[index+1] + '/' + char_array[index+2];
Note that this only works if either the first or second operand in the chain is a std::string
. That will result in the first call to operator+
returning a std::string
, and the rest will follow suit. So this doesn't give the expected results:
std::string signature = char_array[index+1] + '/' + char_array[index+2] + std::string();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With