News:

  • July 03, 2026, 02:56:06 PM

Login with username, password and session length

Author Topic: Removing leading character from a string  (Read 11160 times)

Bobby

  • Sr. Member
  • ****
  • Posts: 60
Removing leading character from a string
« on: August 25, 2015, 09:42:40 AM »
Hello all!

I am taking data in from a metler toledo scale and want to remove a leading period. What I get is this currently in ASCII ".000.00" The leading period is a HEX 02 or <STX> that Metler scales send out.

I am used to using the old co-processor to remove this but am not sure what they best method would be now with the do-more PLC. I did try the "STRTRIM" but looks like that is not one of the included characters that the instruction would trim off.


Thanks in advance for the help!

Bobby

MikeS

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 265
    • Host Engineering, Inc.
Re: Removing leading character from a string
« Reply #1 on: August 25, 2015, 09:48:04 AM »
if you know the "." is always going to be the first character you can use STRSUB - Get Sub-string with an offset of 1.

the safer way is to first use a STRFIND, from the beginning, to locate the ".", this will return the offset from the beginning of the ".". then use this value as the offset in the STRSUB.
Good design costs a lot. Bad design costs even more.

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Re: Removing leading character from a string
« Reply #2 on: August 25, 2015, 09:58:00 AM »
Don't overemphasize the '.'. That is just a place holder for the unprintable 02 hex.
An output is a PLC's way of getting its inputs to change.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6164
  • Yes Pinky, Do-more will control the world!
Re: Removing leading character from a string
« Reply #3 on: August 25, 2015, 10:02:43 AM »
Use STRSUB with an offset of 1.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bobby

  • Sr. Member
  • ****
  • Posts: 60
Re: Removing leading character from a string
« Reply #4 on: August 25, 2015, 10:13:16 AM »
Ha! I looked through them and must have completely skipped that one! Thanks guys! That was easy.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: Removing leading character from a string
« Reply #5 on: August 25, 2015, 10:20:06 AM »
Change the display format in Data View of a STRING to QUOTED TEXT and you will see the escape-sequenced characters (e.g. "$020000.00")

STRSUB - Get Substring, starting at index 1, will give you the number part in its own string.

Then use STR2REAL on the output of STRSUB to get the value as a floating point number.



If you needed to PARSE it, to find the FIRST DIGIT, STRFIND can be used to parse to find the first digit.
I doubt you need this, but I am putting this here in case someone has the situation where they have variable text and need to find the first digit.

In the STRFIND instruction, set the In Offset Start / Out Offset parameter to the value 0 (I will use D42 in this example):
MOVE 0 D42

In STRFIND, set If Found parameter to C42
Find ANY ONE OF THESE CHARACTERS:
"0123456789"

D42 will then equal the index where the first digit occurs.

Use D42 as the Starting Offset in your STRSUB instruction.  Just set to Remainder of Input String.

Then use STR2REAL of the STRSUB Output String being driven by the C42 contact, and it should give you the value as a REAL.  You might want to use a NC C42 contact to set the real to 0 or -1 or some "error" value???

Bobby

  • Sr. Member
  • ****
  • Posts: 60
Re: Removing leading character from a string
« Reply #6 on: August 25, 2015, 11:15:17 AM »
Change the display format in Data View of a STRING to QUOTED TEXT and you will see the escape-sequenced characters (e.g. "$020000.00")

STRSUB - Get Substring, starting at index 1, will give you the number part in its own string.

Then use STR2REAL on the output of STRSUB to get the value as a floating point number.



If you needed to PARSE it, to find the FIRST DIGIT, STRFIND can be used to parse to find the first digit.
I doubt you need this, but I am putting this here in case someone has the situation where they have variable text and need to find the first digit.

In the STRFIND instruction, set the In Offset Start / Out Offset parameter to the value 0 (I will use D42 in this example):
MOVE 0 D42

In STRFIND, set If Found parameter to C42
Find ANY ONE OF THESE CHARACTERS:
"0123456789"

D42 will then equal the index where the first digit occurs.

Use D42 as the Starting Offset in your STRSUB instruction.  Just set to Remainder of Input String.

Then use STR2REAL of the STRSUB Output String being driven by the C42 contact, and it should give you the value as a REAL.  You might want to use a NC C42 contact to set the real to 0 or -1 or some "error" value???

Very helpful thank you! I am finding this do-more to be very powerful and easy to work with so far!