tutorial 01
[[tutorial_01]] last edit on
Sep 4, 2007
11:40 AM
by trevor
Extracting words from two-word strings
I just discovered the WORD$(string, numberofwordsin, delimeter) function so all this is pretty useless now.
This tutorial describes how I break strings of two words, like "hello world", into two words using functions.
So here is the program:
[main]
twoWords$ = " Hello World" 'Has unwanted space at start.
twoWords$ = trim$(twoWords$) 'An example of removing unwanted spaces.
print lefWord$(twoWords$) 'Print the left word.
print rigWord$(twoWords$) 'Print the right word.
function lefWord$(words$)
temWord$ = "" spaFound = 0'Prepare the local variables.
for looper = 1 to len(words$)'Loop for the length of words$.
letter$ = mid$(words$, looper, 1)
'Put the current character in a variable.
if spaFound = 0 and letter$ = " " then
'If it hasn't already found the space and it does.
temWord$ = left$(words$, looper - 1)
'Make a temporary variable equal the part of words$ where'the loop is at.
spaFound = 1
'It has found the space.
end if
next
lefWord$ = temWord$'Return temWord$ (all the letters before the space).
end function
function rigWord$(words$)
temWord$ = "" spaFound = 0'Prepare the local variables.
for looper = 1 to len(words$)'Loop for the length of words$.
letter$ = mid$(words$, looper, 1)
'Put the current character in a variable.
if spaFound = 0 and letter$ = " " then
'If it hasn't already found the space and it does.
temWord$ = right$(words$, len(words$) - looper)
'Make a temporary variable equal the part of words$ where'the loop hasn't been over.
spaFound = 1
'It has found the space.
end if
next
rigWord$ = temWord$'Return temWord$ (all the letters after the space).
end function
end
End of program.
As you can see, my code isn't the neatest. But what happens here is:
- A string variable called twoWords$ is defined as " Hello World".
- It is 'trim$'ed to remove any excess spaces at start and end.
- Its left word, "Hello" is printed, using the function lefWord$.
- Its right word, "World" is printed, using the function rigWord$.
- Within the function, lefWord$, it takes the parameter words$ and returns all the characters up to the first space.
- Within the function, rigWord$, it takes the parameter words$ and returns all the characters after the space.
I think that's all for this tutorial, I hope you understood it. If you don't, or have some suggestions, feel free to contact me on the forum.
I just discovered the WORD$(string, numberofwordsin, delimeter) function so all this is pretty useless now.