This question already has answers here:
using \ in a string as literal instead of an escape
(2 answers)
Back slash causing problems c++
(4 answers)
Closed 3 years ago.
I need a string that only contains "\". But this doesn't seem to work since (as far as I get it) the compiler sees it as a command instead of a simple string without any meaning.
As you can probably tell, I'm still fairly inexperienced, so aplogies if I'm not that up to speed.
I already tried to use a char, but that also didn't work.
What I want is something like this:
std::string mystr = "\";
I get this error message: "A string constant cannot be continued on a second line." Makes sense to me, but doesn't really help me because I'm not even trying to define a string over 2 lines.
You need to escape the backslash:
std::string mystr = "\\";
Alternatively you can use raw string literals:
std::string mystr = R"(\)";
Related
This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();
This question already has answers here:
C++: insert char to a string
(4 answers)
Closed 3 years ago.
I'm struggling with trying to implement a C++ code solution that will allow me to insert a newline (i.e. a string literal '\n') towards the end of a std::string, and not at the very end as most implementations show.
For example, I want to insert a '\n' just -1 characters before the very end itself. So if the string was 100 characters long (poor analogy I know), then I'd like to insert the string literal at the 99th character in a clean, easily readable manner.
Thanks!
Here's one way:
std::string test{"abcdef"};
if (!test.empty())
test.insert(test.length() - 1, "\n");
and here's one based on iterators:
if (!test.empty())
test.insert(std::prev(test.end()), '\n');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am reading some log files. The logs for Windows contain paths like C:\some\path.
When I read with std::getline, I get a string containing unescaped backslashes. How can I replace them with forward slashes?
I tried
std::replace(str.begin(), str.end(), '\\', '/');
but the result looks like C:somepath instead of C:/some/path.
How do I replace the \ with / or \\?
This string is then used to build a JSON object, so not replacing the backslashes results in an invalid JSON object.
The std::replace call that you tried is perfectly valid and should do exactly what you want from it, so the only reason for resulting string not to contain any slashes is there were no slashes of any kind to begin with.
I suggest using a debugger to determine what's going on with your string through its whole lifetime
Okay, so this is for conversion to JSON, where backslashes need to be modified somehow or other (apparently conversion to forward slashes is allowable in this case, otherwise you'd need to double the back-slashes to escape them).
Your basic idea should work--simply replacing each \\ with a / should be easy enough.
#include <iostream>
#include <algorithm>
#include <string>
#include <cassert>
int main() {
std::string in{"a\\b\\c\\d"};
std::replace(in.begin(), in.end(), '\\', '/');
assert(in == "a/b/c/d");
std::cout << in;
}
I'm not sure what problem you encountered--at least for me, this seems to work fine. Of course, this only really makes sense as part of a larger program. If you were going to do this in isolation, tr would be entirely sufficient. If you really needed to make it a program, SNOBOL would do the job considerably more easily than C or C++:
loop: INPUT "\" = '/' . OUTPUT : s(loop)
Escape character like newline using '\n' or backslash like '\\' are only used in literals, i.e. the constant strings and characters in your code. In string or character variables there is no special handling of backslashes.
That's because backslashes in string or character literal constants are handled by the compiler, at the time of compilation. Nothing is done at run-time.
So the solution to your problems is really to do nothing at all.
This question already has answers here:
What are differences between std::string and std::vector<char>?
(5 answers)
C++: char test[100] vs array<char, 100> vs string
(4 answers)
Closed 7 years ago.
It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.
However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?
I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.
To be clear: it is all about expressing the intent.
To put it briefly: if you're storing text, then string, otherwise vector<char>.
This question already has answers here:
What is the C++ equivalent of the C# # symbol prefixing strings?
(3 answers)
Closed 9 years ago.
In C# we can define a complicated string with #:
string str = #"This is the first line.\r\nThis is still the first line";
how about in C++? if we have something like this we don't need to use converting sign '\' for all the special characters.
In C++11 (only) you can use a raw string literal:
std::string str = R"(This is the first line.\r\nThis is still the first line)";