LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Format string to string with leading zeros


gregoryj wrote: 

Edit1: Kudos to James!

 

Edit2: Wait a second, he said from a string to a string.


 

Thanks for the kudos anyways Smiley Wink

 


@Gregory wrote:

@James.M wrote:

I ran a benchmark on the multiple solutions in this thread and there is a very clear winner. Replacing a subset of 8 zeroes wins by a mile:

 


And I learned you can run a For Loop "1M" times instead of "1000000", awesome!

Just change the display format to SI Notation!

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 11 of 20
(2,340 Views)
Kudos for going to the effort of creating a benchmark VI for my fairly un-optimised solution (like I said...brain not working on a Friday afternoon). However, I have to question as you don't do anything with the results in your benchmark that maybe the compiler could optimise out the operations since you don't do anything with them, nor do you display them - so the compiler might see it as dead code. Just a thought...

LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 12 of 20
(2,316 Views)

Oh, good point and it looks like it had an affect. The concatenate solution slows down a bit, the Search/Replace speeds up, and the Replace Subset solution speeds up a bit but is already so fast its hard to tell by how much.

 

Bench 8 out.PNG

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 13 of 20
(2,302 Views)

Sam_Sharp's idea is not that bad if we replace the for loop with initialize array.

2016-02-26 10_23_09-Untitled 1 Block Diagram _.png

Message 14 of 20
(2,289 Views)

@1abvi3w wrote:

Sam_Sharp's idea is not that bad if we replace the for loop with initialize array


Yes! That was what I was looking for! I knew there had to be a way to avoid the for loop (I was hoping for something like 'repeat string' - but initialise array with a concatenate strings works too!).


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 15 of 20
(2,267 Views)

Here's one more option that I think is slightly cleaner and simpler, and should be fast too. Use the version on the left if all inputs strings are no longer than 8 characters, otherwise use the version on the right:

left-pad string with 0s.png

Message 16 of 20
(2,251 Views)

Sometimes it is easier to operate on byte arrays (The 48 is decimal U8):

 

 

 

 

(Still needs some exception handing if the string input can be longer than the total intended length)

0 Kudos
Message 17 of 20
(2,223 Views)

Hi,

Many many thanks to you! I worte this question on Friday by hoping that on weekend it can be answered and on Monday I can look for a solution. But what I see here is awesome 🙂 there is solution even with benchmark! I want to accept all of this as solution but it is not possible to accept all of them 🙂

Thanks again.Smiley Very Happy

0 Kudos
Message 18 of 20
(2,143 Views)

@kahlenberg wrote:

I want to accept all of this as solution but it is not possible to accept all of them 🙂


In fact, you can select multiple replies As Solution. Smiley Happy

 

 

There's a strange phenomenon on this board. Every time a string-related question is asked, there are always many different solutions and different answers given. I think the developers here love a good string parsing challenge!

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 19 of 20
(2,106 Views)

@James.M wrote:
I think the developers here love a good string parsing challenge!

I hate them, because invariably there will be some indecipherable regex solution requiring three paragraphs of explanations.

That's why my solution is basically all blue. I don't like strings. reminds me too much of text based code.... 😄

 

(Just kidding...or am I?)

0 Kudos
Message 20 of 20
(2,092 Views)