Sidebar

How do you remove empty repeat characters?

0 votes
185 views
asked Jan 22, 2022 by daniel-d-7319 (270 points)
I would like to remove the empty repeat characters (~) from the FT1-19 field

FT1|1|||20150508|20150508|CG|99212|Ofc Vst, Est Level II||1||||||^^^3|||v72.3^^I9~789.47^^I9~411.1^^I9~v72.3^^I9~789.47^^I9~411.1^^I9~v72.3^^I9~789.47^^I9~411.1^^I9~v72.3^^I9~789.47^^I9~411.1^^I9~~~~~~~~~~~|hwinston|hwinston||22-1|hwinston|99212|

I have tried using message.removeAllNodes as shown below but that does not seem to do what I hoped

message.removeAllNodes("FT1[" + x + "]-19." + y);

Hoping to have an end result of:

FT1|1|||20150508|20150508|CG|99212|Ofc Vst, Est Level II||1||||||^^^3|||v72.3^^I9~789.47^^I9~411.1^^I9~v72.3^^I9~789.47^^I9~411.1^^I9~v72.3^^I9~789.47^^I9~411.1^^I9~v72.3^^I9~789.47^^I9~411.1^^I9|hwinston|hwinston||22-1|hwinston|99212|

1 Answer

+1 vote

The simplest way to handle this is to pull the entire segment out as a string, parse it into an array (which will remove any empty subsegments), and then loop through the array elements.  As you loop through, you can write each subsegment out to a new variable, then write it back to the message object.

Here's some code to do just that:

// Extract the segment into a string variable 
var ft1Segments = message.getNode('FT1-19');

// Create an array from the string by splitting it based on tildes 
// This will effectively remove the empty subsegments, but will 
// remove the tildes in the process
var ft1Array = ft1Segments.split('~');

// Set up a new empty string
var newFT1Segment = '';

// Loop through your array
for (var x = 0; x < ft1Array.length; x++) {
   // Log each element into the new empty string, adding the tilde back in
   newFT1Segment += ft1Array[x]+ '~';
}

// Finally, set it back to the message, but use the StringUtils.substringBeforeLast()
// function to remove the last tilde, which is unneeded
message.setNode('FT1-19', StringUtils.substringBeforeLast(newFT1Segment, '~'));

answered Jan 24, 2022 by jon-t-7005 (7,590 points)
...