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
I get no errors compiling the simple code with the proper headers. Maybe you've trashed (overwritten) your STL version with an older one where iostreams doesn't overload the std::string type, or your directory paths are pointing to this older version.Quote:
Originally posted by Guysl
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
That's basically the problem with VC++'s "iostream.h" -- it knows nothing about std::string, so there was no operator << written for the VC++'s version "iostream.h".
Regards,
Paul McKenzie
There must be a problem on your system. It compiles fine in my VC6 too. It's in the standard as well (21.2.2).
Is there any perticular reason you are using a char[10] and not a std::string?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());
...
Code:std::string Result;
std::string Str("Hello Gurus, good day!");
int n = 10;
Result = Str.substr(Str.size() - n , n);
Paul and Yves, thanks.
I've reinstalled VC++ , now strings output works fine.
Regards,
Guy
Why using char[10] and not std::string? well, it´s called inexperience.
I could not get my program to accept any of that sintax. I take it was because I did not include the right headers. The code snipped coming from jlou (I think) might shead some light: I am using <string.h>. I will try the one below.
Thanks again.
Luis
PS: btw: while we are at it, what do I have to include to use the CString class?
#include <iostream>
#include <string>
int main()
{
std::string myString = "Hello World!";
std::cout << myString << std::endl;
}
Hi gurus:
The code snipped, along with the "std::string Mystring" worked like a charm... amazing. Just for you guys to get a good laugh, this is what I have to do to get those darn 10 bytes off the string:
// -----------------------------------------------------------------------------------------
// RightS: Copies the rightmost NumChars characters from the string_in to string_out
// -----------------------------------------------------------------------------------------
void RightS(char*string_in,int NumChars, char*string_out)
{
char temp[80];
int length = strlen(string_in);
int i, start, ctr;
start = length - NumChars;
for (i = 0; i <= NumChars; i = i + 1)
{
ctr = start + i;
temp[i] = string_in[ctr];
}
temp[NumChars+1] = '\0';
strcpy(string_out,temp);
}
You can laugh, no problem. I know it´s bizarre, but.. you know, desperation is the mother of creativity, I guess.
But that little code snipped took it of it elegantly. And yes, it was <string>. the <string.h> it´s for something else... I write off as a good experience.
Thanks again,
Luis