Convert String with Line Breaks to Single-Line String in LabVIEW

Updated Apr 28, 2021



  • LabVIEW

This article explains some of the methods available in LabVIEW to remove the line breaks from strings such as the example shown below in order to display the string as a single line to make processing, logging, or presentation of the string easier.

Example of a string containing line breaks.

Line breaks in strings in LabVIEW are caused by termination characters, ASCII character symbols which the computer running your code interprets as various kinds of line breaks. In order to remove the line breaks from a string, you'll first need to determine what kind of line breaks your string contains. The most common type of line break used in strings on Windows systems is an End of Line character, represented as \r\n in ASCII, but it's possible that your string contains a different kind of line break. you can determine what kind of line break is present in your string by inserting the string into a constant, indicator, or control and changing the display format to Backslash (\) Codes using the right-click menu or the object's Properties window. In the case of the example string in this article, the line break character is a Line Feed character.

String Control Properties Window showing the Backslash (\) Codes display setting.
Example string showing Backslash display.

Once you have determined the type of line break present in your strings, you can use the Search and Replace Pattern VI present in the Additional String VIs and Functions Palette to search your strings for that character and replace it with an Empty String constant. An example of this implementation, and various stages of the output, are shown below.

Block diagram of Line Break search and replace.
Front panel of line break search.

If you know your strings will only have one line break in them, you can use this method without modification. However, if you're unsure of how many line breaks a given string will have, you need to add a way to search the string multiple times for line break characters, as the Search and Replace Pattern VI only replaces the first pattern found. One example of how to implement this using a While Loop and the Match Pattern function is shown below.

Example of line break search that can handle different numbers of line breaks in string.
Output of multi-line break search with many line breaks in string.
Example output of multi-line break search with no line breaks in string.