0

I have a requirement to remove "\n\t" from the input string?

For example,

"lorenda bianco"
<loredana.bianco@yahoo.co.jp>

is invalid input for me. (just \n)

"lorenda bianco"
    <loredana.bianco@yahoo.co.jp>

is valid input for me. (\n\t).

I tried with isspace, but don't know how to make combination of different whitespaces.

I want my output as "lorenda bianco" <loredana.bianco@yahoo.co.jp>.

I am able to get rid of "\n" with the help @404. But still, \t doesn't work neither with his solution nor erase/remove. I am also trying to remove \t with:

s.erase(std::remove_if(s.begin(), s.end(), '\t'), s.end());

which somehow doesn't work. But when I print it shows 4 separate spaces instead of a single tab. May be that's why it doesn't work. It expects single tab instead of 4 consecutive space characters. Don't know how to crack it.

--- Failed attempt to remove \n\t from string (\n works but \t still exists )

str before ws removal: "lorenda bianco"
<loredana.bianco@yahoo.co.jp>

str after ws removal: "lorenda bianco" loredana.bianco@yahoo.co.jp

user1228352
  • 569
  • 6
  • 21
  • It might help to use C++ string::replace. http://www.cplusplus.com/reference/string/string/replace/ – Maurice Kasomwung Nov 21 '20 at 05:55
  • 4
    Please provide [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). It is difficult for anyone to answer conclusively without seeing how you are attempting to read and store the information. Without seeing more a combination of `find_first_not_of` using `whitespace` and then using either `erase` or `substr` seem viable ways to do it. – David C. Rankin Nov 21 '20 at 06:12
  • Updated the example output in question. – user1228352 Nov 22 '20 at 05:18
  • @Elliott Indentations getting screwed up here. \t doesn't work. In short, with your solution \n gets removed but not \t. – user1228352 Nov 22 '20 at 05:50
  • As David said, best practice is to use a [minimal, complete and verifiable working example](https://stackoverflow.com/help/minimal-reproducible-example). I can't reproduce your problem based off what I've read here. – Elliott Nov 22 '20 at 05:51

1 Answers1

0
#include <iostream>

int main()
{
    std::string myString = "lorenda bianco\n\t <loredana.bianco@yahoo.co.jp>";

    std::cout << "Before changing: \"" << myString << "\"\n";


    for (int i=0; i < myString.length(); i++){
            if ( myString[i] == '\n'){
                myString.erase(i, 1);
                i -= 1;
            }
            if ( myString[i] == '\t'){
                myString.erase(i, 1);
                i -= 1;
            }

    }


    std::cout << "After changing: \"" << myString << "\"\n";

    return 0;
}
404 TNF
  • 19
  • 6
  • 1
    @Elliott: Actually, I want to remove \n\t as a combination only as per MIME requirement. Removing \n or \t alone is not important for me right now. – user1228352 Nov 21 '20 at 06:39
  • 1
    That should work then-the second snippet of code. If there is more than 1 combination, remove the break and subtract 2 from i after it runs the if statement – 404 TNF Nov 21 '20 at 06:57
  • Guys, \n is working fine with @404TNF solution. But \t doesn't work. when I print it, it shows 4 separate spaces. Doesn't consider single combo of 4 spaces. – user1228352 Nov 21 '20 at 12:31
  • @404TNF, sorry, there are still bugs in that code. `for (int`... should be `for (unsigned`... Also, your second `if` should be `else if`. example of where the above code crashes: `myString = "\n"` – Elliott Nov 22 '20 at 04:24
  • @404TNF, Here's an example of how to fix it which doesn't require the `--i` then the `++i`: https://codeshare.io/5R3dOL – Elliott Nov 22 '20 at 04:34
  • @Elliott Actually, want to remove tabs only and trying following command: s.erase(std::remove_if(s.begin(), s.end(), '\t'), s.end()); which somehow doesn't work. But when I print it shows 4 separate spaces instead of a single tab. May be that's why it doesn't work. It expects single tab instead of 4 consecutive space characters. – user1228352 Nov 22 '20 at 04:54
  • @Elliott Question is edited. – user1228352 Nov 22 '20 at 05:05