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.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I count the number of *'s in a string?

I need to count the number of * in a string of carakters. And the return the number. 0 for no *'s in the string 2 for two *'s in the string and so on.

It's probably quite simple but I can't figure out how!
0 Kudos
Message 1 of 10
(3,801 Views)
The attached vi will show you one way of achieving this, it's not the unique, perhaps not the best, but can give you some ideas...
Hope this helps
Message 2 of 10
(3,801 Views)
Works like a charm!
0 Kudos
Message 4 of 10
(3,801 Views)
Like this.

james_t_dk wrote in message
news:506500000008000000682E0000-1002247793000@exchange.ni.com...
> I need to count the number of * in a string of carakters. And the
> return the number. 0 for no *'s in the string 2 for two *'s in the
> string and so on.
>
> It's probably quite simple but I can't figure out how!



[Attachment Untitled 10.vi, see below]
0 Kudos
Message 3 of 10
(3,801 Views)
Hello,

Just use 'Match Pattern' in a while-loop. Wire the string to the input of
the while loop and use the shift register inside the while loop to wire to
the string input of match pattern. Match string should be: \* hence '*' is a
special character...
Complete the while loop by wiring a >=0 to 'offset past match' and the while
condition.

Supply an email adres if you want to receive an example...

Gr,
Frank Lubbers
0 Kudos
Message 5 of 10
(3,801 Views)
If you could send an example to bwillis@ucla.edu, i'd much appreciate it.

BW


"Frank Lubbers" wrote in message news:<3bd00eeb$0$19399$e4fe514c@newszilla.xs4all.nl>...
> Hello,
>
> Just use 'Match Pattern' in a while-loop. Wire the string to the input of
> the while loop and use the shift register inside the while loop to wire to
> the string input of match pattern. Match string should be: \* hence '*' is a
> special character...
> Complete the while loop by wiring a >=0 to 'offset past match' and the while
> condition.
>
> Supply an email adres if you want to receive an example...
>
> Gr,
> Frank Lubbers
0 Kudos
Message 7 of 10
(3,801 Views)
Or, to complement CraigGraham's answer, I offer the attached VI which includes two alternatives.
Richard
0 Kudos
Message 6 of 10
(3,801 Views)
James,
Here is another alternative solution. This solution is different in the way that it does not make use of shift registers or loops. I only used built in functions to get the count. I hope this helps. /Mikael
Message 8 of 10
(3,800 Views)
Nice. Probably quite a bit more efficient than previous suggestions, mine
included.

Mikael Garcia wrote in message
news:506500000005000000C1480000-1003545641000@exchange.ni.com...
> James,
> Here is another alternative solution. This solution is different in
> the way that it does not make use of shift registers or loops. I only
> used built in functions to get the count. I hope this helps. /Mikael
0 Kudos
Message 10 of 10
(3,800 Views)
There have been several solutions to the question and I find Mikael's particularly good (see http://exchange.ni.com/servlet/ProcessRequest?RHIVEID=101&RPAGEID=135&HOID=506500000008000000682E0000&UCATEGORY_0=_49_%24_6_&UCATEGORY_S=0 ). Out of curiosity, I've examined the options a little further and found Mikael's answer to be the fastest (in microseconds: 1.56 (Mikael); 2.10 (Richard); 3.08 (Gorka Larrea); 4.42 (Richard); 5.02 (CraigGraham)). May be several of us could benefit from learning of the time penalty in using loops where built-in Vis would do? Richard
0 Kudos
Message 9 of 10
(3,800 Views)