Skip to main content
gamedev.net
🔒 Locked

left(string,2)

Started by penetrator Jan 7, 2002 at 5:26 PM 5 replies 350+ views
jenova
jenova
ummm, whatever "left" means. i''m going to assume you are talking about left justifying a string.

here''s the left justify method from my own string class. i removed the actual references to the class.

  
char *LeftJustify(char *pString, int iWidth)
{
int iCount, iIndex;

/* calculate the number of characters to justify. */
iWidth -= (iIndex = (int)strlen(pString));
/* append trailing spaces. */
for (iCount = 0; iCount < iWidth; iCount++) {
pString[iIndex] = '' '';
iIndex++;
}
/* append the null terminator character. */

pString[iIndex] = 0;
return (pString);
}


ohh, and there is no "left justify" function in the ANSI C library. i couldn''t tell you about ANSI C++ or MFC as i don''t use them, but you might want to check them out.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Colin Jeanne
Colin Jeanne
Actually left takes everything from the left side of the string up to a specified number.

I would do something like:

memcpy(dest_string, src_string, chars_to_copy);

Invader X
Invader''s Realm
scaught
scaught
And with STL strings:

  
string src("test");
string dst;

dst = src.substr( 0, 2 );
ragonastick
ragonastick
I''m not sure, but is it possible that some of those equivalents need to be testing for whether the string is too short?

so: Left$("Hi", 3) should return "Hi" (which is the behaviour with the VB Left$

(also, note the $ sign, if you were going to do the C++ version of Left, it returns a VB variant which is poo)

Trying is the first step towards failure.
Trying is the first step towards failure.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.