10 Replies Latest reply: Jul 11, 2012 11:39 AM by 939520 RSS

    Issue with splitting string into multiple lines

    948658
      Hi Experts,

      I have a long string s. I want to split this string into several lines each having 72 characters. For this I have done following programming:


           String s = "For the first time it includes a supplementary report attempting to determine if extreme weather
      events can be linked to human-induced climate change. The research team, including members
      of the UK Met Office, identified recent episodes of extreme weather then used a computer to
      estimate the likelihood of the episode happening in a world without increased CO2." ;

           s = s.replaceAll("[\r\n]+", " ");
           
           char[] sAr = s.toCharArray();
           AbstractList RecepitsList7 = new Bapitrtext.Bapitrtext_List();;
           int start = 0; // start with
           int i = 72 ;
      //     for (int i = 71; i < sAr.length; i++) {
      while (i < sAr.length) {
      //     {     if (sAr[i] == ' ')
                {      
                     Bapitrtext Recepits7 = new Bapitrtext();
                     Recepits7.setTextid("TEXT");
                     Recepits7.setTextline(s.substring(start, i).replaceAll("[\r\n]+", ""));
                     RecepitsList7.add(Recepits7);
                     start = i+1;
                     i += 72;
                     
                     } }
           input.setText(RecepitsList7);

      The output of program is as below:

      For the first time it includes a supplementary report attempting to dete
      mine if extreme weather events can be linked to human-induced climate
      hange. The research team, including members of the UK Met Office, iden
      ified recent episodes of extreme weather then used a computer to estim
      te the likelihood of the episode happening in a world without increased


      Here as we can see last characters are getting deleted by program. For example in first line
      determine 'r' is missing in extreme right.
      In 3rd line c is missing in extreme left (word change is needed. Program
      prints hange.

      Also in 3rd line extreme right 't' is missing
      Also in 4th line extreme right 'a' is missing


      Also the last line is not being printed. In last line "CO2." " should appear.

      There are two issues:
      1. Why last word of each sentence is not being printed?

      2. Why last sentence is not being printed?

      I am struggling since last 2 days. Please help. I made all changes like replacing 72 by 71 etc.
      By making such changes, I am able to print first line correctly. However under all cases
      last character of 2nd line onward is not getting displayed.

      Also last line is not getting displayed. I did every thing like using while instead of for loop etc.

      Kindly help.

      Regards,
      Gary

      Edited by: 945655 on Jul 10, 2012 11:39 PM
        • 1. Re: Issue with splitting string into multiple lines
          TPD-Opitz
          Welcome to the forum.

          Please read this before posting:
          https://wikis.oracle.com/display/Forums/Forums+FAQ

          What you most likely want is a "worwrap". So you should deal with words rather than single characters
          StringBuilder newString = new StringBuilder();
          StringBuilder 
          for (String word :oldString.split("[::space::]")) {
            if (MAX_LINE_SIZE > currentLine.size()+ word.length()+SPACE_SIZE){
              currentLine= new StringBuilder();
            } else {
              currentLine.append(SPACE);
            }
            currentLine.append(word);
          }
          newString.append(currentLine);
          bye
          TPD
          • 2. Re: Issue with splitting string into multiple lines
            948658
            Hi TPD,

            I don't need wordwrap. I need to split a string into multiple lines. Could you kindly see my code and suggest?

            Regards,
            Gary
            • 3. Re: Issue with splitting string into multiple lines
              gimbal2
              I'm not going to read that mess. Use \
               tags and repost the code. The formatted code from the file, don't copy/paste the junk from your first post.                                                                                                                                                                                                                                                                                                                
              • 4. Re: Issue with splitting string into multiple lines
                948658
                String s = wdContext.currentContextElement().getExpense_Text();
                     s = s.replaceAll("[\r\n]+", " ");
                     
                     char[] sAr = s.toCharArray();
                     AbstractList RecepitsList7 = new Bapitrtext.Bapitrtext_List();;
                     int start = 0; // start with
                     int i = 72;

                while (i < sAr.length) {

                          {      
                               Bapitrtext Recepits7 = new Bapitrtext();
                               Recepits7.setTextid("TEXT");

                               System.out.println(s.substring(start, i));
                               RecepitsList7.add(Recepits7);
                               start = i+1;
                               i += 72;
                               
                               } }
                     input.setText(RecepitsList7);

                Edited by: 945655 on Jul 11, 2012 2:25 AM
                • 5. Re: Issue with splitting string into multiple lines
                  sabre150
                  This can all be done in 1 line of code :-
                     String[] lines =  s.replaceAll("\r\n"," ").split("(?<=\\G.{72})");
                  Note - it is only slightly more difficult to break at word boundaries.

                  Edited by: sabre150 on Jul 11, 2012 10:25 AM

                  If one wants to get rid of "\r\n" pairs one might also want to merge multiple white space into a single space. Rather than explicitly look for "\r\n" then one just uses
                       String[] lines =  s.replaceAll("\\s+"," ").split("(?<=\\G.{72})");
                    
                  • 6. Re: Issue with splitting string into multiple lines
                    TPD-Opitz
                    945655 wrote:
                    I don't need wordwrap. I need to split a string into multiple lines.
                    I'm pretty sure that this will produce some unnessesary work for the receiver to make a parsable or at least readable document.

                    But I'd say that <tt>StringBuilder.insert(int offset, String str)</tt> will be your friend.

                    bye
                    TPD
                    • 7. Re: Issue with splitting string into multiple lines
                      948658
                      Hi,

                      I have to create seperate lines. As I am using the code in SAP Java Web Dynpro. Can we have sepearte using while or for loop.

                      Regards,
                      Gary
                      • 8. Re: Issue with splitting string into multiple lines
                        sabre150
                        945655 wrote:
                        I have to create seperate lines.
                        My line of code does exactly that!
                        As I am using the code in SAP Java Web Dynpro.
                        Can we have sepearte using while or for loop.
                        Once you have the lines in an array you can do whatever you like with them.
                        • 9. Re: Issue with splitting string into multiple lines
                          TPD-Opitz
                          945655 wrote:
                          I have to create seperate lines. As I am using the code in SAP Java Web Dynpro. Can we have sepearte using while or for loop.
                          My last hint here is <tt>String.substring(int start, int end)</tt>.
                          List<String> lines = new ArrayList<>();
                          for (int i = 0, oldString.length()>i*72;i++){
                            lines.add(oldString.substring(i*72,(i+1)*72)); // maybe you need one character less
                          }
                          bye
                          TPD
                          • 10. Re: Issue with splitting string into multiple lines
                            939520
                            You say you don't need wordwrap, you need to split a string into multiple lines. I think wordwrap is the best way to do it. I suggest creating a list of words and populating a line with words until you reach the character limit for that line. I suspect you may be overly attached to your initial coding attempt rather than use wordwrap. Also, I don't believe this line 's = s.replaceAll("[\r\n]+", " "); ' will work. It will tend to combine the last word on a line with the first word on the next line without a space between them. If you really want to complicate your life, you may also consider that words are sometimes hyphenated and continued onto the next line. You may want to deal with that situation too. Also, I'll have to take points off if you have any leading or trailing spaces on a line after you create it. Another concern: often there is a blank line in text to separate two paragraphs. You may want to preserve that line as-is.