What does this mean?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Guest

What does this mean?

Post by Guest »

What does LTRIM$ , RTRIM$, and STR$ mean?
example...LEFT$(LTRIM$(RTRIM$(STR$(q))), 1)
What would that do?
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

QB's inline help is your friend. So is your teacher... mabye.

LTRIM cuts off all left spaces. RTRIM cuts off all right spaces EG

Code: Select all

PRINT LTRIM$(RTRIM$("     I have extra spaces!             "));
Would ouput "I have extra spaces!". As for STR$, I dunno off hand. You can look it up.
Image
User avatar
Kyle
Veteran
Posts: 107
Joined: Thu Apr 14, 2005 2:41 pm

Post by Kyle »

Converts a variable into a string. Adds a leading space for signing (+/-)

Code: Select all

string$="Test "+LTRIM$(STR$(123))
PRINT string$
User avatar
Xerol
Veteran
Posts: 81
Joined: Tue Jan 04, 2005 6:27 pm
Location: Timonium, MD
Contact:

Post by Xerol »

Kylemc wrote:Converts a variable into a string. Adds a leading space for signing (+/-)

Code: Select all

string$="Test "+LTRIM$(STR$(123))
PRINT string$
Except string is a reserved keyword and that code would never run :wink:
If you need music composed in MP3, WAV, or MIDI format, please contact me via email.

Xerol's Music - Updated Regularly!
User avatar
Kyle
Veteran
Posts: 107
Joined: Thu Apr 14, 2005 2:41 pm

Post by Kyle »

You get the idea.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Re: What does this mean?

Post by Rattrapmax6 »

Anonymous wrote:What does LTRIM$ , RTRIM$, and STR$ mean?
example...LEFT$(LTRIM$(RTRIM$(STR$(q))), 1)
What would that do?
LTRIM$ trims off wightspaces(or spaces) left of a string:

Code: Select all

String = "     Hello     "

NewString = LTRIM$(String)

NewString = "Hello    "
RTRIM$ does the same just for the right side:

Code: Select all

NewString = "    Hello"
STR$ turns a numerical variable to a string.

Code: Select all

Var = 100

String = STR$(Var)

String = "100"
As for the code:

Code: Select all

q = 200

PRINT LEFT$(LTRIM$(RTRIM$(STR$(q))), 1)
Output is: 2

:wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Let me walk you through Ratt's example, step by step as it evaluates the statement.

Code: Select all

q = 200 

PRINT LEFT$(LTRIM$(RTRIM$(STR$(q))), 1)
1. It takes the STR$ of 200, which gives us a string of 4 characters, a space and 200.

2. Next it does the RTRIM$ which doesn't do anything because we have no trailing spaces. So we still have a space and 200.
3. Now it does the LTRIM$, which removes the leading space. We now have 3 characters, 200.

4. Now it does the LEFT$ on these 3 characters grabbing only the first character as specified by the ",1". So, the final result is 2.

It performs these functions in that order because, in the order of the parenthesis, each function needs a value or a string on which to operate. The STR$ goes first since it has the value of q to work with. Then the RTRIM$ works on the string result of the STR$, and so on.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

I was hoping to spark creativity to figure why it does it.... But you are probaly right to explian it for him, it is rather a tricky code being all in one line... :roll:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Guest

Post by Guest »

Thanks. You all helped me out a lot.
Post Reply