-2

How to remove a specific line from a CString? This line contain: "Line to remove".

For example, the input is:

Remove Specific'\n'
Line to remove'\n'
Line From '\n'
CString C++.

The output should be:

Remove Specific'\n'
Line From '\n'
CString C++.
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91

3 Answers3

1

Try CString.Replace method to find "Line to Remove" and replace with NULL.

Check MSDN

Elixir Techne
  • 1,848
  • 15
  • 20
  • You really don't want to pass `NULL` to `Replace`. – tenfour Mar 20 '14 at 09:48
  • Yes, But the input string is having multiple lines stored in single string object. "Line to Remove" was taken as an example in the initial question and hence mentioned it in my comment. – Elixir Techne Mar 20 '14 at 09:51
  • Yea, I edited my comment; OP is not very clear what he really wants :) But you should still not pass NULL. – tenfour Mar 20 '14 at 10:07
0

You need to copy the content line by line to other new string except the line you want to delete then again copy the new string in old one.

Heena Goyal
  • 382
  • 3
  • 17
0

thanks evry one, the way to do it is like Heena Goyal said

CString oldString = _T("Remove Specific'\n' Line to remove'\n' Line From '\n' CString C++.");
    CString auxString ;
    CString newString =_T("");

    for (int i = 0; i < oldString.GetLength(); i++)
    {
        if (oldString[i] == '\n' || i == oldString.GetLength()-1)
        {
            if( i == oldString.GetLength() - 1)
                auxString += oldString[i];

            if(auxString.Find("Line to remove") == -1)
            {
                newString += auxString +'\n';
            }
            auxString = _T("");
        }
        else
        {
            if (oldString[i] != '\r')
                auxString += oldString[i];
        }
    }
    newString = newString.Left(newString.GetLength() -1);