0

I'm trying split some text typed into a text edit control into individual words to put them in an array.......any newline characters should be put into an array as well. The problem is I'm having trouble detecting newline characters along the way...

What is the correct way to do this?

I've tried the following:

for(int i = 0; i < text.GetLength(); i++)
{
    m_RichEditor.SetSel(0 + i, i + 2); //from start to end character
    m_RichEditor.GetSelectionCharFormat(cf);


    if(m_RichEditor.GetSelText() == "\n") {
        OutputDebugString((CString)"here");
    }

}

but this doesn't work....

user2361103
  • 97
  • 2
  • 12
  • Given the total lack of information and context it's hard to say for sure but it looks like your code will only detect if the current selection includes a newline, and only a newline. It sounds like what you want to have happen is call a member function that returns all the text, not just selected text, and then search through that text for a newline character, instead of simply comparing to one. – Praetorian Dec 20 '13 at 00:12
  • Sorry I've edited my original question to include more info, but from the way I've written it, eventually getseltext() will have selected the newline character and only the newline character....so I don;t see whats going wrong.... – user2361103 Dec 20 '13 at 00:22
  • Isn't your code selecting 2 characters at a time? Try changing the `SetSel` call to `m_RichEditor.SetSel(i, i + 1);` – Praetorian Dec 20 '13 at 00:33
  • yes, i tried that originally. it didn't work so I thought maybe "\n" was considered 2 characters.. – user2361103 Dec 20 '13 at 00:34
  • No it isn't, the backslash is considered an escape character and gives a different meaning when combined with the following character. Anyway, this seems like a very inefficient way to find text you're interested in. Assuming you're using a `CRichEditCtrl`, you could try using the [`FindText`](http://msdn.microsoft.com/en-us/library/1fw2tk36.aspx) member function instead. – Praetorian Dec 20 '13 at 00:36
  • problem is i'm trying to get all instances of "\n", not just one.. – user2361103 Dec 20 '13 at 00:40
  • @user2361103: Use `FindText` anyway, and iterate over them all – Mooing Duck Dec 20 '13 at 00:43
  • That's fine, give it the entire range the first iteration, and after the first result change the range argument so that you start at the character immediately following the last one found. – Praetorian Dec 20 '13 at 00:44
  • -_-.....i actually havent expalined what i'm doing fully.....i'm actually trying to get each word and put it into an array....as well as newline characters..so I don't think FindText would be the way to go....besides....i don;t see why this method wouldn't work... – user2361103 Dec 20 '13 at 00:48

1 Answers1

1

It seems, for RichEdit 2.0, the character for newline is not "\n"....it is "\r"!

user2361103
  • 97
  • 2
  • 12