Split string by word and break line by max length - The Strategic ERP Advisor

The Strategic ERP Advisor

Strategic ERP Solutions and Consulting

Breaking

Wednesday, July 15, 2020

Split string by word and break line by max length

Split string by word and break line by max length

Nghia Song - Technical Consultant
   public container MergeSplitWordsToLines(container  inputStrings, int maxLength)
    {
        str currentLine = "";
        str separator = " ";
        container breakLines = conNull();
        str strL;
        int i = 1;

        for(i = 1; i <= conLen(inputStrings); i++)
        {
            strL = conPeek(inputStrings, i);
            if (strLen(currentLine) == 0)
            {
                currentLine =  strL;
            }
            else if (strLen(currentLine) + strLen(separator) + strLen(strL) > maxLength)
            {
                breakLines += currentLine;
                currentLine = strL;
            }
            else
            {
                currentLine += separator + strL;
            }
        }

        if (strLen(currentLine))
        {
            return breakLines;
        }
        return breakLines;
    }

    public container SplitStringByWords(str inputString, int maxLength)
    {
        container splitWords = conNull();
        container splitLines = conNull();

        splitWords = str2con(inputString, " ");

        if (conLen(splitWords))
        {
            splitLines = this.MergeSplitWordsToLines(splitWords, maxLength);
        }

        return splitLines;

    }