PWP wiki processor

stringhelp

| StartPage | WikiPages | AdditionalFiles |

Help with strings
 -How can I fix this ?
 -What tricky Stuff ?

Help with strings

Strings are usually given a set length. For example you may dimension a string as follows :

MyWord as STRING * 20

This will create a variable called MyWord and give it a length of 20 characters.

Consider this code :

MyWord as STRING * 20
MyWord = "Hello"
SearchWord$ = "Hello"
if MyWord = SearchWord$  then
  Print "They are the same"
Else
  Print "They are different"
End if

If you run this You will get "They are different" ... Why are they different ??

They are different becasue the two string variables are of different length

The command MyWord = "Hello" will result in the variable being stored as Hello############## where # represents NULL or empty characters.

Now is you compare MyWord with another string, for example :

SearchWord$ = "Hello", it wont work. This is because SeachWord$ is of a different length ...

How can I fix this ?

There is two ways :

  1. Always declare variables and set them to have the same length before comparing.
  2. So tricky stuff to compare the strings.

What tricky Stuff ?

In the example above, the only letters inside MyWord that are significant is the LEFT most 5 characters ... H.e.l.l.o.

To compare MyWord with SearchWord$, you need to find the significant letters in the string. Here's how ...

First find out how long the string is ...

SearchLength = LEN(SearchWord$)

Next, find the left most letters in the string we are comparing this to ...

LEFT$(MyWord,SearchLength)

Top put it all together ....

MyWord as STRING * 20
MyWord = "Hello"
SearchWord$ = "Hello"
SearchLength = LEN(SearchWord$)
if LEFT$(MyWord,SearchLength) = LEFT$(SearchWord$,SearchLength)  then
  Print "They are the same"
Else
  Print "They are different"
End if

This will print ..."They are the same!!"

   (Powered by PWP Version 1.4.2)