News:

  • October 18, 2026, 07:26:00 PM

Login with username, password and session length

Author Topic: How to split up a string separated by delimiters  (Read 13120 times)

RBPLC

  • Hero Member
  • *****
  • Posts: 586
How to split up a string separated by delimiters
« on: December 02, 2020, 08:34:09 AM »
Thanks franji,
Now for a follow on question. I looked at Garry's example that you linked. He's getting Modbus ASCII data and can get substrings based on character position. My question is this:

What would be the best way to get a substring from a variable length ASCII stream with variable length substrings that has a field separator? For example:

First stream: xx.xx xx.xx xxx.xxx x.x
Second stream: x.xx xx.x xx.x xx.xx

The data in this format represents sensor parameters with a space separating each sensor parameter. As the data comes in, leading values may change because of range of value and trailing values might be rounded. The space character separates each field regardless of each substring length. What's the easiest way to get each field regardless of its length when a space always separates the fields?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: How to split up a string separated by delimiters
« Reply #1 on: December 02, 2020, 08:57:14 AM »
I split this question out since it is a great question by itself, and it is not related to Modbus.

I wrote a Tokenize program a while back (DmD 1.3) that utilized STRFIND in a REPEAT/UNTIL loop in a TASK that looked at comma separated numeric values, and sticks the strings into a separate string block.  I also wrote another TASK in the same program that parses those numeric strings into their equivalent numeric values.

I want to clean it up and post it on the Examples forum, so it may be a couple days.

deep6ixed

  • Hero Member
  • *****
  • Posts: 105
Re: How to split up a string separated by delimiters
« Reply #2 on: December 02, 2020, 09:45:31 AM »
Could that be used to strip specific characters from a string and replace them with other characters in new string?

SS1 = "This is a Message" could be processed to SS2 = "This%20is%20a%Message"

Is that possible also with something similar.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: How to split up a string separated by delimiters
« Reply #3 on: December 02, 2020, 10:57:09 AM »
Is that possible also with something similar.
You could repurpose it, yes.
OR, use it as a building block.

The example takes your text, say "1, 2.4, 49, -1"
and separates them based on comma/whitespace into a data block of (up to 100) strings called Tokens
Tokens0 = "1"
Tokens1 = "2.4"
Tokens2 = "49"
Tokens3 = "-1"
Tokens4..99 = ""
and NumTokens is set to 4.

So you could THEN write another subroutine that takes Tokens block and NumTokens and inserts "%20" of each token and appends it to SL0

STRCLEAR SL0
FOR V0 0 NumTokens-2
STRPRINT SL0 Append Tokens[V0] "%20"
NEXT
STRPRINT SL0 Append Tokens[V0+1]  // the last one does not need a "%20"


RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to split up a string separated by delimiters
« Reply #4 on: December 02, 2020, 12:06:41 PM »
Related to this thread:
I need to send "M1!" to this device. When using PuTTY, I type "M1!" and then press Enter. The command is sent to the device and a response is returned that I can see in the PuTTY window. Pressing the Enter key initiates the request with PuTTY. When I try to do the same thing using the STREAMOUT function, I can't get the device to respond. I would think that sending the following would be equivalent to what I've done in PuTTY:

STRPRINT SS0 "M1!" "$R"
STREAMOUT SS0

This is not working for me. Any ideas?

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6177
  • Yes Pinky, Do-more will control the world!
Re: How to split up a string separated by delimiters
« Reply #5 on: December 02, 2020, 12:16:29 PM »
Related to this thread:
I need to send "M1!" to this device. When using PuTTY, I type "M1!" and then press Enter. The command is sent to the device and a response is returned that I can see in the PuTTY window. Pressing the Enter key initiates the request with PuTTY. When I try to do the same thing using the STREAMOUT function, I can't get the device to respond. I would think that sending the following would be equivalent to what I've done in PuTTY:

STRPRINT SS0 "M1!" "$R"
STREAMOUT SS0

This is not working for me. Any ideas?

$N? Or even $R$N?
"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

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: How to split up a string separated by delimiters
« Reply #6 on: December 02, 2020, 12:26:20 PM »
$N? Or even $R$N?

Probably needs to be $N, which is CR/LF
$N converts to $0D$0A
$R converts to $0D
$L converts to $0A

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6177
  • Yes Pinky, Do-more will control the world!
Re: How to split up a string separated by delimiters
« Reply #7 on: December 02, 2020, 12:52:18 PM »
$N? Or even $R$N?

Probably needs to be $N, which is CR/LF
$N converts to $0D$0A
$R converts to $0D
$L converts to $0A

Ah...confusing with C strings. "\r\n" gets used in all the TCP stuff.
"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

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to split up a string separated by delimiters
« Reply #8 on: December 02, 2020, 06:13:27 PM »
It helps when you have the baud rate set correctly  :o.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to split up a string separated by delimiters
« Reply #9 on: February 16, 2021, 10:47:33 AM »
I split this question out since it is a great question by itself, and it is not related to Modbus.

I wrote a Tokenize program a while back (DmD 1.3) that utilized STRFIND in a REPEAT/UNTIL loop in a TASK that looked at comma separated numeric values, and sticks the strings into a separate string block.  I also wrote another TASK in the same program that parses those numeric strings into their equivalent numeric values.

I want to clean it up and post it on the Examples forum, so it may be a couple days.

1) Were you able to get to this example?
2) Where is the tokenize program for dealing with the .csv?

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3850
    • Host Engineering
Re: How to split up a string separated by delimiters
« Reply #10 on: February 16, 2021, 12:05:44 PM »
Sorry - I found it in my 2.5 projects.  See the Tokenize post under the Examples forum:
https://forum.hosteng.com/index.php?board=20.0