Before You Post, Please Note...

-> Please Note: By Posting, you agree to submit the contents of your post to the Public Domain <- SEE: howto post to this blog

Friday, February 2, 2007

puppybasic intro ch. 08 - strings part ii

green text is being edited and should not be considered  
relevant to the tutorial.
for a while, it will make up
the bulk of this section.

black text has been edited to be more accurate with
regards to puppybasic.

........................................................................
strings part ii: searching and manipulation
1 of 8




an intro to puppybasic

chapter 8: strings part ii







........................................................................

........................................................................
strings part ii: searching and manipulation
2 of 8

although you could find a better routine to do the same thing, INSTR is
a very fast and simple way to see if and when a string exists as part of
a larger string: for instance:

suppose q$ is storing the string:
"the quick brown fox jumped, and stuff."

and you want to know if the string (let's pretend you don't know what q$
is; maybe it's from a file or the keyboard.) contains the word "fox" so
you code:



........................................................................

........................................................................
strings part ii: searching and manipulation
3 of 8

n = INSTR(q$, "fox")

n will be equal to the number of letters, numbers, and punctuation that
you have to read before you've started reading "fox"... in this case,
the "f" in "fox" is the 17th byte in q$, so INSTR will set n to 17. if
the string "fox" is not found in the string that q$ is storing, n will
be set to 0.
1
123456789012345 7 90123456789012345678
"the quick brown fox jumped, and stuff."



........................................................................

........................................................................
strings part ii: searching and manipulation
4 of 8

SPACE$

is really, really easy to use. SPACE$(n) is a string of ascii 32 (space)
that is n bytes long. q$=q$+SPACE$(10) will add 10 spaces to the end of
q$.

LEFT$ and RIGHT$

are not as powerful or complete as MID$ but are slightly easier to use.
skipping straight to examples, LEFT$("Seventy",5) is "Seven" and
RIGHT$("elephant",3) is "ant". MID$ is similar:


........................................................................

........................................................................
strings part ii: searching and manipulation
5 of 8

the syntax for the MID$ function is:

MID$(q$, whichbytetostartat, howmanybytes)

so n$=MID$("hello", 2, 4) would set n$ to "ello" and
n$=MID$("yeah, or... something", 14, 2) would set n$ to "om".

there is a MID$ statement as well, which works the same way, except
instead of reading a string it will rewrite it:




........................................................................

........................................................................
strings part ii: searching and manipulation
6 of 8

y$="yeah, or... something"
n$=SPACE$(2)+"no"

MID$(y$, 13, 4)=n$

will store "yeah, or... nothing" in y$. the 4-byte "some" has been
replaced with 2 spaces and "no", changing "something" to " nothing".
whoosh!





........................................................................

........................................................................
strings part ii: searching and manipulation
7 of 8

if you do not like the MID$ statement for rewriting, it is totally
possible to avoid it entirely using only the MID$ "reading" function
and concatonization of strings.

there are times (especially with code like the first menu routine,
earlier) when your variable will have leading or trailing spaces that
you don't want. when you use STR$(n) to convert a numeric variable to a
string for instance, it will contain a leading space (if it is at least
zero) for a plus sign, even though the plus sign will never show. if the
number drops below zero, a negative sign will show, but otherwise, you
may be stuck with a space you don't want, like this: " 55".


........................................................................

........................................................................
strings part ii: searching and manipulation
8 of 8

LTRIM$(STR$(n))

will remove the leading spaces from a number, and LTRIM$(RTRIM$(q$))
will remove the spaces from both ends of any string. this is not only
useful when changing numbers to strings, but also when accepting
keyboard input or data from files.


-= end of chapter 8 =-




........................................................................
an intro to puppybasic (chapter 8)
this content is public domain.
........................................................................


click here to go back to the contents page

1 comments:

Anonymous said...

Pills For Suicide Sources fartonast
Buy Valium Online Without Prescription
Valium acts upon the brain by affecting the neurotransmitter gamma-aminobutyric acid, and bringing about a calm euphoric state over the user.
[url=http://www.lianesorenson.com/]Buy Generic Valium[/url]
You may be wondering more about how Valium can help you with your social anxiety disorder or insomnia so we will outline some pros and cons of the medication below.
http://www.lianesorenson.com/ - Generic Valium Online
If you begin to experience adverse side effects let your doctor know immediately so he can make the proper adjustments.


cheap citalopram
Buy Celexa (Citalopram) online and save your money.
[url=http://www.naplesdirectory.org/]citalopram drug[/url]

How Celexa works?
For brain to communicate with other parts or within itself it uses special chemical called serotonin, which acts as a neurotransmitter.
http://www.naplesdirectory.org/ - cheap celexa

Celexa is an SSRI (Selective Serotonin Re Uptake Inhibitor) category medicine and can be used to reduce the cholesterol content in the blood.