Page 1 of 1

string if statement

Posted: Thu Jun 02, 2011 10:32 am
by achillis1
Hello,

I create a string of default value of 20.
Then I want to have a desicion icon that when the string has a certain value then folow the ''yes'' route. But when I state if; string[] = 33 then it shows error. It wants to state as if string[x] =33.


Also if I read the USB serial port (read string) and have the input data(e.g hello) be assigned to a string named Mystring; then how do I say to flowcode the following: if Mystring = hello > do that or .....

Thank you,

Best Regards,

Andreas Achilleos

Re: string if statement

Posted: Thu Jun 02, 2011 11:15 am
by Benj
Hello Andreas,

You can use the compare function in the string manipulation icon.

test = Compare$(string, "33", 1)

if the strings are equal then test will equal 0.

Here is the Flowcode help file regarding the compare function.

Compare$(string1, string2, compare_type)
Compares the two strings, parameters 1 and 2, and returns a BYTE value corresponding to the following results:

0 = strings are identical
1 = string1>string2
255 = string2>string1

The 3rd parameter, compare_type, determines whether or not the check is case sensitive. values for compare_type are:

0 = case sensitive
1 = case insensitive.
Examples

Str1 = "ABC"
Str2 = "abc"

RetVal = Compare$(Str1, Str2, 0)

RetVal is now 255 as Str2 is later in the ASCII sequence.

RetVal = Compare$(Str1, Str2, 1)

RetVal is now 0 as when case insensitive the two strings are identical.

Str2 = Str1
RetVal = Compare$(Str1, Str2, 0)

RetVal is now 0 as both strings are identical.

Re: string if statement

Posted: Thu Jun 02, 2011 12:23 pm
by achillis1
Thanks Ben!!!

I've got another one: If I want to store a single byte value e.g 155 to a string then I will have to create a string of 1 caharacter long i.e string[1]?

Then if I want to store the word '' hello '' I would create a string of 5 characters i.e string[4]?
If I say Mystring[20] = 255 or MYstring[20]= '' yes'' then what array characters are occupied and the remaining what values do they got?
Regarding the example Mystring = ''yes'' I do this in the string manipulaton icon and says that I have an undefined variable and if I state Mystring = 4 then it says that I have a wrong type variable.

Thank you

Re: string if statement

Posted: Thu Jun 02, 2011 1:01 pm
by Benj
Hello,

A string is quite simply an array of bytes.

So a string of 20 characters is actually 20 bytes numbered 0 - 19.

eg

string[0] = x
...
string[19] = y

To store Hello you would need a string[5] eg 5 characters.
string = "hello"

string[0] = 255 loads the byte value 255 into the first byte of the string.

string[0] = "Yes" This will not work and is not recommended

string[0] = 'Y'
string[1] = 'e'
string[2] = 's' is permitted

or

string = "yes"

Re: string if statement

Posted: Thu Jun 02, 2011 1:19 pm
by achillis1
Thanks Ben,

About the string = ''yes''
I do this in the strring manipulation icon and does not accept it! It says that the line contains an undefined variable and means the ''yes''

Re: string if statement

Posted: Thu Jun 02, 2011 5:09 pm
by Frank123
I have same problem.

Re: string if statement

Posted: Thu Jun 02, 2011 5:38 pm
by Frank123
Ok i didn't use S= icon.

Re: string if statement

Posted: Thu Jun 02, 2011 6:00 pm
by Frank123
if i send a string >127 char to LCD flowcode crash.

Re: string if statement

Posted: Thu Jun 02, 2011 7:56 pm
by medelec35
achillis1 wrote:Thanks Ben,

About the string = ''yes''
I do this in the strring manipulation icon and does not accept it! It says that the line contains an undefined variable and means the ''yes''
You would get that message if you placed String="Yes" in a calculation box.
You need to place String="Yes" within a string manipulation box e.g:
String_Yes.jpg
String_Yes.jpg (7.88 KiB) Viewed 8775 times
Martin

Re: string if statement

Posted: Fri Jun 03, 2011 6:54 am
by achillis1
OK, the problem was that the punctuation marks I used were created by the wrong key!
If you see in the first screenshot the space between the punctuation is bigger than the space in the second screenshot!

So if anybody else is having the same problem then it would be best to start looking for the right key! On my keyboard the right quotation marks is generated by shift+2.

Thank you.

Re: string if statement

Posted: Fri Jun 03, 2011 10:00 am
by Benj
Hello,

The single quotation is used to denote a character.

For example using a calculation icon you can do this.

bytevar = 'a'
bytevar = bytevar - '0'

The double quotation is used to denote a string.

For example in the string manipulation icon you can do this.

string = "hello" + " world"
string = string + "..."