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: 

Scan From String - Remove Comma

Solved!
Go to solution

Hello LabVIEW community.  Here's an easy one that's got me a little stumped.  So obviously, not easy for me.  I'm trying to use Scan From String to get the City, State, Zip from a string.  I can build the string as I have just described with commas using Format Into String.  But you can't do it the other way around and get an error 85.  Yes, I have read the white paper on this and am not entirely sure how to apply their solution.  Yes, I could put the bandaids on the code and remove the comma "manually" but I was wondering if the community knew how to remove the commas from the string by just using Scan From String.

Address1.JPG

Address2.JPG

0 Kudos
Message 1 of 12
(7,249 Views)
Solution
Accepted by topic author DailyDose

Scan From String is 'greedy' as you have discovered.  %s will basically grab anything it can, including the commas so the first %s will just grab the whole string.  You need to put the brakes on it just a little bit.  For simple cases like this (no commas allowed inside the individual parts) simply replace %s,%s,%s with %[^,] ,%[^,] ,%[^,] which means match anything which is not a comma followed by a comma.

Message 2 of 12
(7,238 Views)

You da man!  But one more question, let's say it was City, Zip, State.  And I wanted to output the zip as an I32 and not a String?

0 Kudos
Message 3 of 12
(7,232 Views)

@DailyDose wrote:

You da man!  But one more question, let's say it was City, Zip, State.  And I wanted to output the zip as an I32 and not a String?


Never mind.  I answered my own question.  %d is only grabbing numbers so " %[^,] ,%d,%[^,] "  would work just fine.  Thank you!

Message 4 of 12
(7,229 Views)

Well, here's te solution just using Scan From String, which you made a precondition.  Not saying it's the way I'd necessarily do it.  There are also Match Pattern and Match RegEx nodes, and even Scan String for Tokens, but using the right format specifier on Scan From String Will do.  Note the format specifier is

 

%[^,],

 

which says "match all chars that aren't a comma", then match a literal comma (which is discarded).

 

Hope this helps!

Dave

EDIT: I hate it when I'm five minutes too late and miss the good discussion. Smiley Sad 

 

David Boyd
Sr. Test Engineer
Abbott Labs
(lapsed) Certified LabVIEW Developer
Message 5 of 12
(7,225 Views)

@Darin.K wrote:

%s will basically grab anything it can, including the commas so the first %s will just grab the whole string.


Just a slight correction.  The %s in a Scan From String actually stops at a white space (ex: space, tab, etc).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 12
(7,220 Views)

You might want to also include the space in the format string so that it doesn't get included in the return values.  So as Darin said, everything that isn't a comma followed by a comma and a space. 

 

%[^,] , %[^,] , %d

(There is a space after the second and fourth comma.)

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 7 of 12
(7,209 Views)

EDIT: I hate it when I'm five minutes too late and miss the good discussion. Smiley Sad 

 


I gave you a Kudo.  🙂

0 Kudos
Message 8 of 12
(7,202 Views)

Also late, but I'd use the Spreadsheet String to Array, where the delimeter is the comma.  Then you get an array of strings with each element being the three fields.  You can then index the zip and turn it into a number if you want.  Not sure which performs better.

Message 9 of 12
(7,173 Views)

Quick test shows the Array to String method, with a string to number, being about 15-20% faster than the scan from string for 100,000 items.

 

93ms for Scan from String, and 74ms for Array to String.

Message 10 of 12
(7,168 Views)