Hitachi Vantara Pentaho Community Forums
Results 1 to 4 of 4

Thread: Cut in string at X characters but not inside words

  1. #1
    Join Date
    Mar 2015
    Posts
    23

    Question Cut in string at X characters but not inside words

    Hello all,

    I'm looking to do a certain transformation to a long string text. I have a string of for example 70+ Characters and want to limit it to always maximum 70 characters. If there are more characters it should split it to the next row and continue to do so until the string is cut in lengths of 70 characters max.

    This is not difficult to do with a cut in strings step, however the cut in string step could potentially cut in the middle of a word. I do not want this. It should break up the string after a space, so in my case the first space at the end before 70 characters. This way words will not be broken up and all sentences are nicely split.

    Could anyone give an example of this with one of the steps, or a javascript for this?

    Thanks in advance!
    Last edited by Dutchsky; 10-12-2018 at 05:05 AM.

  2. #2
    Join Date
    Aug 2016
    Posts
    235

    Default

    Why so many insist on javascript? It would be fairly easy to do in java!
    Code:
        
    //Add import to the top:
    import java.util.ArrayList;
    
    private static String[] splitString(int splitLength, String text)
    {
        ArrayList<String> stringList = new ArrayList<String>();
        while(text.length() > splitLength)
        {
            String subText = text.substring(0, splitLength);
            int lastSpaceIndex = subText.lastIndexOf(' ');
            if(lastSpaceIndex == -1)
            {
                    lastSpaceIndex = splitLength;
            }
            subText = text.substring(0, lastSpaceIndex);
            stringList.add(subText);
            text = text.substring(lastSpaceIndex, text.length());
        }
        if(text.length() > 0)
        {
            stringList.add(text);
        }
        return stringList.toArray(new String[0]);
    }
    You can then get the output as as String array. Test example:

    Code:
    String input = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    int stringLength = 70;
    String[] output = splitString(stringLength, input);
            
    for(int i = 0; i < output.length; i++)
    {
        System.out.println(i + ": " + output[i]);
    }
    Output:
    Code:
    0: Lorem Ipsum is simply dummy text of the printing and typesetting
    1:  industry. Lorem Ipsum has been the industry's standard dummy text
    2:  ever since the 1500s, when an unknown printer took a galley of type
    3:  and scrambled it to make a type specimen book. It has survived not
    4:  only five centuries, but also the leap into electronic typesetting,
    5:  remaining essentially unchanged. It was popularised in the 1960s
    6:  with the release of Letraset sheets containing Lorem Ipsum passages,
    7:  and more recently with desktop publishing software like Aldus
    8:  PageMaker including versions of Lorem Ipsum.
    Last edited by Sparkles; 10-12-2018 at 10:19 AM.

  3. #3
    Join Date
    Mar 2015
    Posts
    23

    Default

    Hello Sparkles thanks for your reply! I am not too familiar with this yet, could you explain how I can create a .ktr sample with your example if I simply create a data grid and call the column input and use your test string.

    Do I use the Modified Java Script Value step (Im only a little bit familiar with this step), which parts of your code should I exactly input there?

    Thank you

  4. #4
    Join Date
    Aug 2016
    Posts
    235

    Default

    Quote Originally Posted by Dutchsky View Post
    Hello Sparkles thanks for your reply! I am not too familiar with this yet, could you explain how I can create a .ktr sample with your example if I simply create a data grid and call the column input and use your test string.

    Do I use the Modified Java Script Value step (Im only a little bit familiar with this step), which parts of your code should I exactly input there?

    Thank you
    No, you don't use java script (why is this always insisted on?). You use java! The step is called User Defined Java Class (UDJC). In that step, you can create whatever java code you want. Really useful when you want to handle data in such detail as you describe here.

    How to use UDJC step has better discussions elsewhere, you can also find excellent tutorials like this:

    http://type-exit.org/adventures-with...va-class-step/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Privacy Policy | Legal Notices | Safe Harbor Privacy Policy

Copyright © 2005 - 2017 Pentaho Corporation. All Rights Reserved.