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, '~'));