Obtener la posición final de CString

How can I find the position of the end of a CString? I am trying the

int end = str.Find(L'\n');

but it returns -1. I want in particular to get a substring from a CString from some position in the CString till the end of it. The problem is I cannot get the position of the end. Any suggestion?

preguntado el 02 de julio de 12 a las 11:07

Not every string ends with '\n'. With CString.GetLength() you can get the length of the string, which is also the end of the string. -

Not only do strings not end with a \n, they may contain any number of \n. e.g. "Hello\nWorld\n". Which CString are you using? See: cplusplus.com/reference/clibrary/cstring -

3 Respuestas

Utiliza CString::Mid(beginPos), ya que CString sub = str.Mid(begin). It will return the substring that starts at beginPos and stretches all the way to the end of the string. If you do want the string's length, use GetLength. Buscando '\n' is pointless, as it might not be in the string, and if it does, it might not be at the end of that string.

Respondido 02 Jul 12, 12:07

Looking up the character L'\n' searches for a line break rather the the 'end' of the string. Doesn't CString support a size() or length() method, you can use to get the overall length that can be used to calculate the length of the substring you want?

Respondido 02 Jul 12, 11:07

First, the string is zero terminated, so at least you should be finding '\0' instead of '\n'.

In fact, you chould check the size of the string and decrease it by 1, and that's the index position of the last char.

Respondido 02 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.