Topic: string and Left method
Author: "Al Stevens" <alstevens@midifitz.com>
Date: 1999/11/27 Raw View
>I have not found any similar methods for the
>string class. I have tried the copy method but it
>does not add terminating 0. As the standard
>includes lots of generic stuff it is hard to know
>if there is some algorithm, iterator, etc. than
>can be used as replacement for Left, Right and
>Mid.
Use the std::string::substr and str::string::size member functions.
CString mstr;
std::string str;
mstr.Left(ct) becomes str.substr(0, ct)
mstr.Mid(first,ct) becomes str.substr(first, ct)
mstr.Right(ct) becomes str.substr(ct, str.size() - ct)
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Adrian Stanley" <Adrian.Stanley@Marlborough-Stirling.com>
Date: 1999/11/27 Raw View
Look up substr().
sedlar@my-deja.com wrote in message <81g613$as4$1@nnrp1.deja.com>...
>I am changing an old application and have then
>choosen to use the string class available in
>Standard C++.
I have not found any similar methods for the
>string class.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/11/27 Raw View
On 27 Nov 1999 08:08:47 GMT, "Al Stevens" <alstevens@midifitz.com>
wrote:
: Use the std::string::substr and str::string::size member functions.
:
: CString mstr;
: std::string str;
:
: mstr.Left(ct) becomes str.substr(0, ct)
: mstr.Mid(first,ct) becomes str.substr(first, ct)
: mstr.Right(ct) becomes str.substr(ct, str.size() - ct)
Since length defaults to string::npos, the last one can be
str.substr(ct)
Now the only change in parameters is the 0 for left.
John
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Darin Adler <darin@bentspoon.com>
Date: 1999/11/29 Raw View
Al Stevens <alstevens@midifitz.com> wrote:
>> mstr.Right(ct) becomes str.substr(ct, str.size() - ct)
That's wrong.
mstr.Right(ct) becomes str.substr(str.size() - ct)
Right is supposed to get you "ct" characters from the end of the string. Al
Stevens' suggested implementation for Right gets the string after removing
the first "ct" characters, which is not the same thing.
-- Darin
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]