From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

scan from string with delimiters

I want to scan a string which is made from a fixed number of strings comma separated.

For example

source string = str1,str2,str3,str4

I want to extract the four strings "str1", "str2", "str3" and "str4".

 

The problem is that one (or more of the strings) can be empty.

For example

source string = str1,str2,,str4

I want to extract the four strings "str1, "str2", "" (i.e. empty string), "str4"

All the functions I tested (sscanf, Scan , strtok) handle multiple occurrences of delimiter as single occurrence and so I get

"str1", "str2", "str3

 

Which function can I use?

Vix
-------------------------------------------
In claris non fit interpretatio

-------------------------------------------
Using LV from 7
Using LW/CVI from 6.0
0 Kudos
Message 1 of 5
(5,795 Views)

Hello vix!

 

Unfortunately, the strtok function will not be able to detect empty tokens.

However, if your requirement is to detect empty tokens, it's not very hard to implement such a function of your own: a simple implementation of such a function would be using a for-loop which scans the input string, and extracts substrings beginning at the last position of the "," and ending at the position of the next ",". You probably also need to perform various limit use cases (e.g. empty input string, input string ending with delimiter, etc.).

 

Best regards!

- Johannes

0 Kudos
Message 2 of 5
(5,781 Views)

Hello Johannes,

I know I can quite easily implement my own function to parse the string, but this is true for many of the existing functions inside a CVI library Smiley Wink

 

I also know strtok isn't able to find empty tokens, but what about Scan()?

As far as I understand from the help, it should be able to do this. Or not?

Vix
-------------------------------------------
In claris non fit interpretatio

-------------------------------------------
Using LV from 7
Using LW/CVI from 6.0
0 Kudos
Message 3 of 5
(5,778 Views)

The Scan function isn't able to detect an empty character string, as it will detect your second "," as the beginning of your next token.

0 Kudos
Message 4 of 5
(5,772 Views)

I use Scan() with the option %s[xt44]

and from CVI Help

 

tn Terminate on character ...

When applied to a target string that the functions fill from a source string, the t modifier specifies that the functions stop filling the target on the first occurrence of the character n, where n is the ASCII value of the character. Thus, %s[t59] causes the functions to stop reading the source string on an ASCII semicolon. More than one t modifier can occur in the same specifier, in which case the functions stop filling the target when any of the terminators occur. If no t modifier exists, the functions stop filling the target on any whitespace character.

 

As far as I understand, this description means that Scan() should be able to detect an empty token.

Am I wrong? Why?

Vix
-------------------------------------------
In claris non fit interpretatio

-------------------------------------------
Using LV from 7
Using LW/CVI from 6.0
0 Kudos
Message 5 of 5
(5,761 Views)