Split string by right most space

 A utility to split note on the right most space before the set limit number of characters is reached

//    Peter Lykkegaard, 22 mar 2019
//    -----------------------------
//    Split long note on right most space before limit is reached
//    TODO! Check parameters if they are of valid type/content
//
//    Name: splitNoteBySpace
//    Description: Method to split a given string by latest space before length limit
//    Parameters
//        input$, alphanumerical / String which needs to be split
//        max$, alphanumerical / Max characters on each line
//    Output, alphanumeric / array / Array of strings to return

local output$[];

#importJavaStart
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
#importJavaEnd

#javastart

    final String string = _StrVar_INPUT.getString();
    final String max = _StrVar_MAX.getString();
    final String regex = "(.{1," + max + "})(?:\\s|$)";

    Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    Matcher matcher = pattern.matcher(string);
    
    int idx = 0;     // Entries in output    
    while (matcher.find()) {
        for (int i = 1; i <= matcher.groupCount(); i++) {
            _StrVar_OUTPUT.getJuVar(idx, 2).setString(matcher.group(i).toString());
        }
        idx++;
    }
#javaEnd
    
exitProc(output$);