Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Data Types and Arrays

Strings and Paths

Work with LabVIEW strings and paths: concatenate, format, scan, search, split and parse text, handle line endings, and build portable file paths.

Instruments speak in text, log files are text, and users type text. LabVIEW has a rich set of string functions, plus a separate Path type for file locations. This lesson covers both. LabVIEW cannot run on this site, so the code examples are JavaScript stand-ins that reproduce what the functions do. They are not LabVIEW output.

The string type

A string wire is pink. A LabVIEW string is a sequence of bytes, usually interpreted as ASCII or the system code page. That is important: a string can hold arbitrary binary data, such as a packet from an instrument. Modern versions can also work with Unicode if you enable it (Tools > Options > Environment has a Unicode setting, and many string functions have Unicode-aware options). Keep this in mind when working with non-English text or binary instrument data.

A string control or indicator has display modes (right-click > Normal Display, '' Codes Display, Password Display, Hex Display). \ Codes Display shows non-printing characters: \n for line feed, \r for carriage return, \t for tab and \00 for byte zero. It is the best way to see what really is in a string from an instrument.

Core string functions

In Programming > String:

  • Concatenate Strings joins inputs. The node grows to take more inputs.
  • String Length.
  • String Subset takes a substring by offset and length (starting at 0).
  • Match Pattern and Search and Replace String find text, with regular-expression support.
  • To Upper Case and To Lower Case.
  • Trim Whitespace.
  • Split String divides at an index.
  • Spreadsheet String To Array and Array To Spreadsheet String convert between delimited text (CSV-style) and arrays.
  • Number To Decimal String, Fract/Exp String To Number and other conversions.

Format Into String and Scan From String

These are the workhorses.

Format Into String works like printf: you give a format string with specifiers and wire values to match. Examples (from the standard LabVIEW format syntax):

  • %d integer, %f fixed decimal, %e scientific, %g general float, %s string.
  • %.3f three digits after the decimal point.
  • %5d width of five.
  • %x hexadecimal.
  • Temperature: %.1f degC builds a message.

Scan From String does the reverse: it reads values from text according to a format string. %f,%f reads two numbers separated by a comma. If the text does not fit, an error is set, and defaults are returned. The related Scan String For Tokens pulls the first token. Always handle the error output.

Formatting text

Format Into String Numbers to text
Scan From String Text to numbers
Concatenate Join pieces

Check the error output of Scan.

1 / 2

Line endings and terminators

Instruments often end each message with a termination character: line feed (\n, byte 10), carriage return (\r, byte 13), or both (\r\n). A frequent bug is a stray \n that stays on the end of a reply, so the number you scanned is fine but a string comparison fails. Use \ Codes Display to see it, and wire End of Line (Programming > String > Additional String Constants) to build messages in a platform-independent way. Trim whitespace from replies before comparing.

Arrays of strings and the Delimited approach

For a line of CSV text such as 12.5,13.1,12.9, use Spreadsheet String To Array with a comma delimiter to get a numeric array directly. To write one, use Array To Spreadsheet String, with a format such as %.3f. For more complex formats, loop over lines: Search and Replace String can replace commas with tabs, and Match Pattern can split at a delimiter.

Paths

A path wire is teal/green. A path is not a string: it is a platform-neutral file location with a known structure (drive, folders, file). Functions in Programming > File I/O > File Constants give you common locations: Application Directory, Temporary Directory, Default Data Directory, Current VI's Path.

Main path functions:

  • Build Path joins a base path and a relative name. Use it instead of concatenating strings with a backslash, which breaks on other operating systems.
  • Strip Path returns the parent folder and the last name.
  • Path To String and String To Path convert when needed (for example, to display a path or to read one from a settings file).
  • Not A Path is a special value that means "no path." A Path Control returns it when empty, and many file functions open a file dialog when given Not A Path.
  • Get File Extension and Split Path examine a file name.

Good practice:

  • Build paths relative to a known folder, such as the application directory, rather than hard-coding C:\Users\You\....
  • When your VI becomes an executable, the layout inside the built application changes. Use Current VI's Path with Strip Path to find your own folder, and test the built app, not just the VI in the development environment. See the deployment chapter.
  • Never assume a drive letter exists.

Stand-in 1: Format and Scan

This models LabVIEW's % format syntax for a few common specifiers. It is a teaching stand-in, not the full language.

JS Stand-in: format into string, scan from string

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in 2: terminators and splitting a reply

This models a typical instrument reply, with a trailing line feed, and shows how to see it and clean it, plus how to split a CSV line into numbers.

JS Stand-in: stray terminators and CSV

Press Run (or Ctrl + Enter) to see the console output here.

No output.

When a string comparison "should" match but does not, switch the string to \ Codes Display. A hidden space or line feed is almost always the reason.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What color is a string wire?

    Strings are pink; paths are teal.
  2. 2. Which display mode reveals line feeds and carriage returns?

    \ Codes Display shows non-printing characters as \n, \r and so on.
  3. 3. Why use Build Path instead of concatenating strings?

    Build Path avoids hard-coded separators that break on other platforms.
  4. 4. What does Scan From String do when the text does not match the format?

    It reports an error; always check it.

Summary

  • Strings are byte sequences (pink); paths (teal) are a separate type.
  • Format Into String builds text from numbers; Scan From String parses text and flags errors.
  • Check for stray line terminators with \ Codes Display and trim before comparing.
  • Use Build Path and standard directories for portable file locations.
  • Test file access in the built application, not only in the development environment.

Next: Arrays and Auto-Indexing