cancel
Showing results for 
Search instead for 
Did you mean: 

splitting up a string into seperate elements

IBAHKA
Member

splitting up a string into seperate elements

Hello, I am receiving data in strings from the serial port, however I would like to split up the strings into seperate elements- each element in the string is divided by \s, and the end of the string has \r\n. I'd like to put the data in an array with the dimension number being the number or elements the strings- 7. How can I do this? Thank-you!
15 REPLIES 15
John Rich
Active Participant

Re: splitting up a string into seperate elements

First, use the Search and replace string function using \r\n as the search string and an empty string as the replace string. Next use the Spreadsheet string to array function with \s defined as the delimiter. This will give you an array of strings. Both of these are found on the string pallette. This method will work even if the number of elements in your data string changes.
altenbach
Knight of NI

Re: splitting up a string into seperate elements

Message contains an attachment
"Spreadsheet string to array" will convert it to a numeric array, which is only appropriate if you are dealing with numbers only. I assume you want an array of strings as output.

If there are always exactly 7 elements, you could just blindly use a FOR loop as in the attached example. If the number of elements is variable, just add a bit of logic to detect the end of the string.
John Rich
Active Participant

Re: splitting up a string into seperate elements

Message contains an attachment
"Spreadsheet string to array" is a polymorphic function which works with strings as well as numbers as in the attached example (version 6.1).
altenbach
Knight of NI

Re: splitting up a string into seperate elements

Ahh .... I don't remember seeing that, but it makes sense. ;-)
Isn't LabVIEW cool?
Highlighted
Cory_K
Active Participant

Re: splitting up a string into seperate elements

I am doing a similar task, and used the VI you provided.
Using this, each piece of data is seperated.
How can I index each one, so I can send that data to the appropriate place?
For example, if the 5th one is my O2 measurement, how can i pull only that number out so I can do something with it?
Cory K
RavensFan
Knight of NI

Re: splitting up a string into seperate elements

Message contains a hyperlink

Use the Index Array Function.

Array functions are LabVIEW fundamentals.  I would recommend looking at the online LabVIEW tutorials
LabVIEW Introduction Course - Three Hours
LabVIEW Introduction Course - Six Hours

Tags (1)
Ray.R
Knight of NI

Re: splitting up a string into seperate elements

Message contains a hyperlink

And more training here:

To learn more about LabVIEW, I suggest you try looking at some of these tutorials.
You might also try having a look at these video tutorials..

R

Tags (1)
JCollado
Member

Re: splitting up a string into separate elements

Message contains an attachment

 

Thank  you

 

Just what I was looking for. I was familiar with this in VB.net but not in labview.

 

Here is a snippet of my code. The reference came from local variables when snippet was made.

 

The function is to read a text file written by another application. Basically arguments that I'm reading from a text file instead of reading them from the command arguments.

 

I did not need the replace VI.

 

Thanks and I hope this help

JCollado
altenbach
Knight of NI

Re: splitting up a string into separate elements

Message contains an image Message contains an attachment

 


@JCollado wrote:

 

Here is a snippet of my code. The reference came from local variables when snippet was made.


 

You still make quite a few mistakes and add unnecessary complexity.

 

  1. Never operate on paths as strings!!!  Use "build path" as shown.
  2. To read the entire file, you can use the atomic read function (top image). No need to open, measure, read, close. Even better, Use "read from spreadsheet string" (bottom image).
  3. To get successive elements, you don't need to wire the indices.