Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip leading characters from a string 3

Status
Not open for further replies.

David Higgs

Programmer
May 6, 2012
390
GB
My application reads the Frequency of my Transceiver via a serial port. The format of the Frequency for example is “FA007074000” or “FA144174000” I would like to strip the “FA” plus any leading Zeros (1 or 2 in my case) to produce a Frequency Reading of “7074000” and “144174000”. Actually I’ll use Transform(frequency,"@R 999.999.999") to produce “7.074.000” and “144.174.000”
 
Left trim does what you need to, as you can provide the alternative string that you are looking to trim/strip, instead of the default (space).

You can use LTRIM() twice, first to trim the "FA" from the beginning and then again to strip the x number of "0" after that. Shown below in the sample function.

Below I'm only showing the trimming, you could also add the transform into the function as well if you can get it working as you desire. I put it into the function but noticed that because it works from the left to right, on the shorter frequency, where the zeroes have been stripped out, the transform produces 707.400.0, not the 7.074.000 that you say you are looking for. So I commented it out on the sample. You might have to do something else to ensure the 7-character frequency is formatted as X.XXX.XXX instead of XXX.XXX.X but I'll let you decide what is best there:

Code:
? ConvertRawToFrequency("FA007074000")
? ConvertRawToFrequency("FA144174000")

Function ConvertRawToFrequency
    Lparameters tcRaw

    Local lcReturn
   
    lcReturn = Ltrim(ltrim(tcRaw, 1, "FA"), 1, "0")
   
    *lcReturn = Transform(lcReturn,"@R 999.999.999")

    Return lcReturn

Endfunc
 
If the FA is always the first two characters, the easiest way to strip them would be:

Code:
lcFreq = SUBSTR(lcFreq, 3)

That will still leave you with the leading zeros. You can use LTRIM() to get rid of them:
Code:
lcFreq = LTRIM(lcFreq, 0, "0")

Check the Help if you need any more info about how to use those functions.

Mike
 
just quick'n30
Code:
Mstring = "FA007074000"
DO WHILE !BETWEEN(LEFT(Mstring,1), "1", "9") .and. !EMPTY(Mstring)
    Mstring = SUBSTR(Mstring,2)
?mstring
ENDDO
 
Last edited:
You could use the double CHRTRAN trick to keep only characters specified, like only digits:

Code:
cString = "FA007074000"
cFreq = CHRTRAN(m.cString,CHRTRAN(m.cString,"0123456789"),"")
Which keeps all digits and removes any non digits.

The leading 0 are kept. It's no good idea to exclude 0 from the filter, as it would remove all 0s, not just leading zeros.
But is stripping off the leading zeros really good? A TRANSFORM can get rid of leading spaces, if you want.
Well, or VAL() (converting to a number) would do so:

Code:
cString = "FA007074000"
cFreq = CHRTRAN(m.cString,CHRTRAN(m.cString,"0123456789"),"")
nFreq = VAL(cFreq)
 
Last edited:
Thank you all for your replies, I now have a working system.

Code:
lcFrequency = this.Input    &&    Read Transceiver "Frequency"
    
lcFrequency = SUBSTR(lcFrequency,3,9)

lcFrequency = LTRIM(lcFrequency,0,"0")

DO CASE                 
    &&    160 to 40 Metre Bands
    CASE VAL(lcFrequency) < 9000000
    Txcvr_Freq = Transform(lcFrequency,"@R 9.999.999")
    &&    20 to 6 Metre Bands
    CASE VAL(lcFrequency) > 9000000 AND VAL(lcFrequency) < 100000000
    Txcvr_Freq = Transform(lcFrequency,"@R 99.999.999")
    OTHERWISE
    &&    VHF & UHF Bands
    Txcvr_Freq = Transform(lcFrequency,"@R 999.999.999")       
ENDCASE

    thisform.lbl_freq.Caption = (Txcvr_Freq)        &&    Display Frequency on Form

You might have to do something else to ensure the 7-character frequency is formatted as X.XXX.XXX instead of XXX.XXX.X but I'll let you decide what is best there

Thank you Paul, I made use of DO CASE / Endcase.
 
1. Transform actually works even better with formatting output if the input parameter is a value, not a string.
2. You never use cFrequency, always VAL(cFrequency). So consider
Code:
lnFrequency = VAL(LTRIM(lcFrequency,0,"0"))
Then use lnFrequencyin the rest of the code.
 
Code:
My application reads the Frequency of my Transceiver via a serial port. The format of the Frequency for example is “FA007074000” or “FA144174000” I would like to strip the “FA” plus any leading Zeros (1 or 2 in my case) to produce a Frequency Reading of “7074000” and “144174000”. Actually I’ll use Transform(frequency,"@R 999.999.999") to produce “7.074.000” and “144.174.000”

cStr = "FA007074000"
Cfreq= transform(VAL(SUBSTR(cstr,3)),"@R 999,999,999") && " 7,074,000"
Freq = LTRIM(STRTRAN(cfreq,',','.')) && '7.074.000'
 
cStr = "FA007074000"
Cfreq= transform(VAL(SUBSTR(cstr,3)),"@R 999,999,999") && " 7,074,000"
Freq = LTRIM(STRTRAN(cfreq,',','.')) && '7.074.000'

Thank you Steve, that's cut the line count down a bit.

Code:
lcFrequency = this.Input    &&    Read Transceiver "Frequency"

 cStr = lcFrequency
Cfreq = transform(VAL(SUBSTR(cstr,3)),"@R 999,999,999") && " 7,074,000"
Txcvr_Freq = LTRIM(STRTRAN(cfreq,',','.'))                 && '7.074.000'

thisform.lbl_freq.Caption = (Txcvr_Freq)        &&    Display Frequency on Form

Had a little bit of work further down stream, all working ok on the various Frequency Bands that I use.
 
David,

You can use LTRIM() with distinct trim characters and even strings.

So, simply
Code:
? LTRIM("FA144174000", 1, "FA", "0")
? LTRIM("FA007074000", 1, "FA", "0")
 
This sequence seems to do what you want.

lcTest="FA007074000"
? lcTest && FA00707400
lcTest=IIF(LEFT(lcTest,2)="FA",SUBSTR(lcTest,3),lcTest)
? lcTest && 00707400
lcTest=LTRIM(lcTest,1,"0","")
? lcTest && 707400

G.
 
David,

You can use LTRIM() with distinct trim characters and even strings.

So, simply
Code:
? LTRIM("FA144174000", 1, "FA", "0")
? LTRIM("FA007074000", 1, "FA", "0")
António, Nice to hear from you again; thank you for your reply. I will follow up on your advice and have a good read of the help files. I've used most of the 'Trim' commands in the past but have not really looked any deeper than the basic command.
 
This sequence seems to do what you want.

lcTest="FA007074000"
? lcTest && FA00707400
lcTest=IIF(LEFT(lcTest,2)="FA",SUBSTR(lcTest,3),lcTest)
? lcTest && 00707400
lcTest=LTRIM(lcTest,1,"0","")
? lcTest && 707400

G.
Gary, Thank you for your advice. Although I now have a working system I will trial your code.
 
Thank you to everyone that has replied to my request for help, much appreciated. I was almost too embarrassed to ask the question but I'm pleased I did with all of the replies.
 
VAL(SUBSTR(cstr,3)) surely is the shortest way to get out the number, when you know for sure you always have digits in position 3 and all after that.

If there ever is a change and nomenclature would become a 3 letter prefix it's easy to fix to SUBSTR(...,4) instead. But stability of code against changes is also always a worth a consideration, string filtering for digits only is the simplest way to strip off any other characters, no matter how many. There's also STRFILTER within Foxtools.fll for that matter.

The only risk of removing all non digits is that in strings containing two numbers separated you merge them into one, i.e. if you ever would get two frequencies clumped together as "FA007074000 FA144174000", removing all non digits gets you "007074000144174000" and not two numbers.

If there is a change in nomenclature, you'll likely have to adjust code anyway, so no big deal.
 
Last edited:
Hi Chriss,

The command “FA;” is sent to the Transceiver COM Port to request the current Frequency and responds with e.g "FA007074000;”. The nomenclature “FA;” is unlikely to change as it’s a standard in use for many years by several manufacturers, so I take your point about using VAL(SUBSTR(cstr,3). I will follow up on your advice and check out the help files. I revisit my code quite frequently to try other ideas to improve efficiency etc and do make use of the suggestions you and others offer, it keeps the aged brain active!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top