How do i "subtract" a character from a string in c++ [duplicate] - c++

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();

Related

Inserting a newline near the end of a std::string [duplicate]

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');

Find sequence of chars in string c++ and erase it [duplicate]

This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 6 years ago.
I need find all occurrences of sequence: \r\n(some hex number)\r\n and delete this sequences from my string. Hexadecimal number doesn't start with 0x or x. It's just 20bb for example.
These sequences are chunks in http 1.1 protocol. I can't find them with string.find, maybe some regex would help.
Thanks for help.
From the code here I made this:
std::string string("\r\n20BB\r\n");
string = std::regex_replace(string,
std::regex("\r\n[0-9A-Fa-f]+\r\n"), "");
It should work. The [0-9A-Fa-f]+ captures one or more hex digits.

Allocating words from a string separated by spaces to a variable? C++ [qt] [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 9 years ago.
I just wanted to know what the technical term for allocating words from a string separated by spaces to a variable is called so I can look up a tutorial for it. Any terms, links, or explanations would be greatly appreciated.
You can do it like:
QString lineText = "some sample words";
QStringList tokens= lineText.split(" ",QString::SkipEmptyParts);
You can do whatever you like with the words in the tokens list.

Is there something equivelant in c++(or c++11) for # like in c#? [duplicate]

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)";

Splitting a String into a Vector C++ [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 9 years ago.
I have been working on the following piece of code; It opens an inputfile, reads the input, and now I wish to take that input (stored in a string) and split it by whitespaces; inputting each element into an integer vector.
I understand how to typecast. The problem is in the splitting. How do I do this efficiently? I would like to use a while loop or for loop except I am unable to index the string. I converted the string to constant char so I could index it but now I am unable to compare the value at a point in the string to the expression " " in my if statement. What should I do?
Compare the current symbol to ' '.