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');
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:
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"(\)";
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.
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 ' '.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
comparing two strings with comma seperated values
I am working in C++, where I have two strings:
string str1 = "1,4,8,",
str2 = "4,1,8,";
Both strings contains comma separated values. Now I just want to check whether all the elements in str1 also exist in str2, regardless of their position. Is there any direct way to check this? Do I need to write custom code for this?
As far as C++ is concerned, those strings are just sequences of characters. If you apply meaning to those characters (such as "comma separated values"), then you'll have to write some code to extract the data and deal with it.
I would do something like:
split the string on ','
convert each sequence of digits into an integer (skipping over empty elements)
insert those integers into a set (one for each input string)
compare the sets
It's up to you to determine what kind of integer to use.
Yes, you need to write custom code, although not a lot of it. Once you figure out the algorithm you can post here if you have further questions on how to implement each part.