Hi gurus:
What is a function that will return the rightmost 10 or n characteres of a string? Like the VB right$ function?
THanks,
Luis
Printable View
Hi gurus:
What is a function that will return the rightmost 10 or n characteres of a string? Like the VB right$ function?
THanks,
Luis
string Str("1234567890123");
int n = 10;
cout << Str.substr( Str.size() - n , n).c_str();
I think there is no need for any conversionQuote:
Originally posted by Guysl
string Str("1234567890123");
int n = 10;
cout << Str.substr( Str.size() - n , n).c_str();
just std::cout<<str.substr(fromhere, tothere);
it is still safe, no harm at all...
1. what "conversion"?Quote:
I think there is no need for any conversion
just std::cout<<str.substr(fromhere, tothere);
it is still safe, no harm at all...
if you are referering 'int n', then its just an example for
a variable use as a parameter.
2. the original request was rightmost characters, so
fromwhere=? tothere=?
3. second parameter of substr is the length, not the end position.
4. (-- deleted -- )
You don't have to call c_str(), since the streams have overloaded operators for std::string. But yeah, it does return the rightmost n characters.
weird, how come VC++ 6 doesn't let me streaming the string without c_str(). are there any compiler switches I have to change?
btw, program is crashing while n>size(), so a correction:
cout << Str.substr( n>Str.size() ? 0 : Str.size() - n , n);
okay, okay, i didnt say anything, why did you get trembling so soon ?Quote:
Originally posted by Guysl
3. second parameter of substr is the length, not the end position.
Fine, I just meant a mask is just for the face not for the whole body, mate !
Look at the way Vyes answered, sure you are gonna be VIP..:) Hello.
No matter who you acutally are, to me you are Guysl, now its my turn to make it right again, sorry for being a stickie pickie.
What is length here ? up there I meant your #3. Okay , please Guysl let me say this is not a correct because it is the end position not the length..
Oh it is really weird why even a newb C++ can also write this thing. I hope there is nothing in there...
Dont worry, it is just an temporarely annoying problem of C++....Later people can learn more about it...
Regards,
-Fiona
PS: You can call me Niner, that is what Robert Lovelace used to call me...:) I am glad to meet you...
Great !Quote:
Originally posted by Guysl
btw, program is crashing while n>size(), so a correction:
cout << Str.substr( n>Str.size() ? 0 : Str.size() - n , n);
LittleShay,
frankly, I didn't understand your last post (except of the smilies ;) )
please accept my apology if I hurt you in some way.
anyway, substr second parameter is not a position but
the number of characters (length):
http://msdn.microsoft.com/library/de...ringsubstr.asp
Regards,
Guy
Thanks for all the answers. Now, I don“t mean to complicate things but, if I do:
char result[10];
string Str("Hello Gurus, good day!");
int n = 10;
and then;
strcpy(result,Str.substr( Str.size() - n , n).c_str());
Will that add the '\0' at the end of the string?
'cause the idea es to copy a substring to a string. You see, I have a dipslay field (on a console program) that is only 10, and when I try to display the results of a certain event, it overflows the field and overwrites it's neighbors.
Thanks!
Luis
strcpy copies from source until it get a '\0', and it copies it as well.
The c_str() call will add a \0 to the string, i.e. make it a C-String. strcpy will copy everything up to and including the \0 character.Quote:
Originally posted by taguato
char result[10];
string Str("Hello Gurus, good day!");
int n = 10;
strcpy(result,Str.substr( Str.size() - n , n).c_str());
Will that add the '\0' at the end of the string?
In other words, your example above will crash, as you need 11 bytes to store 10 characters plus a \0 and result only reserves 10.
Quote:
Originally posted by Guysl
weird, how come VC++ 6 doesn't let me streaming the string without c_str(). are there any compiler switches I have to change?
Make sure you are using the correct iostream header, and make sure you include <string>. Getting either of those wrong will give you an error in VC++ 6.0.Code:#include <iostream>
#include <string>
int main()
{
std::string myString = "Hello World!";
std::cout << myString << std::endl;
}
Thanks.
I'm using the same headers but keep getting:
error C2679: binary '<<' : no operator defined....
I have the DX9 SDK installed too, with default project setting
uses its libs, could it be the reason? any idea?
Regards,
Guysl