Conversion from string to char – c++

You can get a specific character from a string simply by indexing it. For example, the fifth character of str is str[4] (off by one since the first character is str[0]). Keep in mind you’ll run into problems if the string is shorter than your index thinks it is. c_str(), as you have in your comments, gives you a char* representation (the … Read more

How to convert a char to a String?

You can use Character.toString(char). Note that this method simply returns a call to String.valueOf(char), which also works. As others have noted, string concatenation works as a shortcut as well: But this compiles down to: which is less efficient because the StringBuilder is backed by a char[] (over-allocated by StringBuilder() to 16), only for that array to be defensively copied by the resulting String. String.valueOf(char) “gets in … Read more

std::string to char*

It won’t automatically convert (thank god). You’ll have to use the method c_str() to get the C string version. Note that it returns a const char *; you aren’t allowed to change the C-style string returned by c_str(). If you want to process it you’ll have to copy it first: Or in modern C++: