split a long text into multiple lines of string Array

Hi Folks,

I have a requirement to split a long text into multiple lines. The long text will be in a String and this has to be split into 13 charecter string array with each element being the split string.

For example if I have "The quick fox jumped over a lazy dog"
It should split as
The quick fox
jumped over a
lazy dog

I have the following code which breaks it and displays the items accordingly but this is not capable of inserting individual string elements into a string array.

String s = "The quick fox jumped over a lazy dog";
 
char[] sAr = s.toCharArray();
int start = 0;
        // start with 
for (int i = 13; i < sAr.length; i++) {
    if (sAr[i] == ' ') {
        System.out.println(s.substring(start, i));
        start = i+1;
        i += 13;
    }
}
System.out.println(s.substring(start));

Open in new window

kalyangkmAsked:
Who is Participating?
 
krakatoaCommented:
Sounded to me as if you simply wanted the string split into sections of 13 pieces each, plus any remainder. If so then this may help.

class SplitAt13{


 static String string = "The quick brown fox jumped over the lazy dog";
 static int counter=0;
 static int start=0;
 static String[] sA;
 static final int take=13;
 static int end = take;

 public static void main(String[] args){
    if(string.length()<take){sA = new String[1];sA[0]=string.toString();}
    if((string.length()/take)*take<string.length()){sA = new String[(string.length()/take)+1];}
    else{sA = new String[string.length()/take];}
    
    if(sA.length==1){System.out.println(sA[0]);}
    
    else {
    while(counter<sA.length){sA[counter]=string.substring(start,end);start+=take;counter++;if(counter==string.length()/take){end=start+string.length()%take;}else{end=start+take;}}
    
    }
    
    for(String s : sA){System.out.println(s);}
    
 }
    
}

Open in new window

0
 
CEHJCommented:
Why are you not using

String[] array = s.split(" ");

Open in new window


?
0
 
kalyangkmAuthor Commented:
Hi CEHJ,

I dont have a delimitter to use the split method. If you still think I can use it then I am not sure how to use it to split it for a specific length.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
CEHJCommented:
The delimiter is " ". You can test, but successive concatenation of each tokenized word, the total line length and form a new line if the next line is going to exceed thirteen characters
0
 
kalyangkmAuthor Commented:
Hi CEJH,

Any suggestions on how to do it?
0
 
CEHJCommented:
You can test, but successive concatenation of each tokenized word
=
You can test, with each successive concatenation of each tokenized word
0
 
kalyangkmAuthor Commented:
Hi CEHJ,

Sorry, I can I have a sample code.
0
 
CEHJCommented:
No time now sorry - unless we do a Live
0
 
kalyangkmAuthor Commented:
sure lets do it
0
 
CEHJCommented:
I'm on Live now
0
 
kalyangkmAuthor Commented:
I  just sent a "Hi" in the chat, did you receive it?
0
 
kalyangkmAuthor Commented:
Hi CEHJ,

THe time expired. Please provide whatever you could for me to work.
0
 
CEHJCommented:
import java.util.*;
import java.io.*;

public class SentenceSplitter {
    public static void main(String[] args) {
        System.out.println(SentenceSplitter.splitIntoLines(args[0], 13));
    }

    public static List<String> splitIntoLines(String s, int lengthLimit) {
        List<String> result = new ArrayList<>();
        String LINE_SEP = System.getProperty("line.separator");
        StringBuilder buf = new StringBuilder();

        String[] words = s.split(" ");
        String sep = "";
        for (int i = 0; i < words.length; i++) {
            buf.append(sep).append(words[i]);
            if (buf.length() < lengthLimit) {
                sep = " ";
            } else {
                result.add(buf.toString());
                buf.setLength(0);
                sep = "";
            }

        }
        if (buf.length() > 0) {
            result.add(buf.toString());
        }
        return result;
    }
}

Open in new window

Not tested exhaustively (unlike me - it's 1AM here) but works with your example
0
 
kalyangkmAuthor Commented:
I am getting the result as [The quick fox, jumped over a, lazy dog] which is good. But with my tool I needed something like a list array

So I am able to eventually modify the sample code I have provided earlier as follows where in ResultList is used to return result of a funtion
https://help.sap.com/doc/javadocs_nw74_sps08/7.4.8/en-US/PI/com/sap/aii/mappingtool/tf7/rt/ResultList.html

public void SplitTextLineActual(String[] textLine, int limitLength, ResultList result, Container container) throws StreamTransformationException{

String stest  = textLine[0];


char[] sArray = stest.toCharArray();
int start = 0;
        // start with 
for (int i = limitLength; i < sArray.length; i++) {
    if (sArray[i] == ' ') {
        result.addValue(stest.substring(start,i));
        start = i+1;
        i += limitLength;
    }
}
result.addValue(stest.substring(start));

}

Open in new window

0
 
kalyangkmAuthor Commented:
Hi CEHJ,

Thanks for the solution, always good to learn new stuff.
0
 
CEHJCommented:
But with my tool I needed something like a list array

What's a 'list array'?

Also, please note that i made a small error in the code. Like i described as a necessary step before i posted it, you need to determine whether
nextArrayElement.length() + sep.length() < lengthLimit 

Open in new window

before you append it
0
 
kalyangkmAuthor Commented:
Hi CEHJ,

Sorry, I meant Array list. What I have done in my solution is to use a string which is coming one of the fields of the source XML to split into it based on the criteria I have explained so that the target xml segment gets populated multiple times. Just trying to accommodate the code compatible to my tool using ResultList interface mentioned above in my earlier comment.
0
 
kalyangkmAuthor Commented:
Hi Krakatoa,

Thanks, I will check into your solution.
0
 
CEHJCommented:
Sorry, I meant Array list.
My code does use an ArrayList

Just trying to accommodate the code compatible to my tool using ResultList interface mentioned above in my earlier comment.
You can add just as well to your ResultList as an ArrayList if you prefer to use the former
Just be sure to make the alteration i mentioned above.
0
 
kalyangkmAuthor Commented:
Hi CEHJ,

Sure, let me check how I can modify your code.
0
 
krakatoaCommented:
Error correction :
my line 13 should be "else if(( . . . " instead of a simple "if( . . . ".
0
 
kalyangkmAuthor Commented:
Hi Krakatoa,

Thanks for the solution. I see what you have done. First you have figured out the number of splits and then generated the split. It looks a bit complex for this solution. But anyway overall you got what I needed.
0
 
krakatoaCommented:
It looks a bit complex for this solution. But anyway . . .
Ok.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.