TheByteWorks

    Quality ready made and affordable custom software

Home

Software

PHP Serial
rsterm
Easy Control
Rack Designer
EzCom2Web

Quick Texts
Wallpaper Slideshow

Articles

All articles
WS2300 weather station PHP project
Your own terminal in 17 lines
PC PIC Voltmeter
Network messenger in 22 lines
Phone Dialer

Download
Purchase

Support

Details
E-mail

Copyright 2001-2024
© Cosmin Buhu

  Parsing strings

Parsing strings

How to extract useful data from a string.
Note: this article is obsolete, Easy Control versions 2.0 and up has special functions for parsing strings - split, extract.

If you have to extract a specific data from a string (received from a serial device, over the network
or whatever, you may use this procedure. For example we suppose we received the following string:
data1 data2 data3crlf
where between data is a space and the string is terminated with a crlf sequence (ascii 13,ascii 10).
The simple idea is to search for the space position and copy data until there, then copy the remaining
string, search again,.., until no space is found so we have the last data.
This procedure is easy to accomodate with other string/data formats.

Easy Control code:

#simulate the string received
recdata="123 1023 525"&chr(13)&chr(10)
#we have to cut 2 final chars (cr,lf) so will substract 2
length=len (recdata)-2
result=mid (recdata,1,length)
#check for the first space
posofspace=pos (" ",result)
#cut till there
value1=mid (result,1,posofspace-1)
#Do what you want with value1
writelog ("Data1 is "&value1)
#we need the rest of the string
length=len (result)
result1=mid (result,posofspace+1,length)
#check again for the first space (in fact the second in the original string)
posofspace=pos (" ",result1)
#cut till there
value2=mid (result1,1,posofspace-1)
#Do what you want with value2
writelog ("Data2 is "&value2)
#the remaining is of course the third result
length=length=len (result1)
value3=mid (result1,posofspace+1,length)
#Do what you want with value3
writelog ("Data3 is "&value3)

Enjoy !