7

I looking for a way to split my chunk of string every 10 words. I am working with the below code.

My input will be a long string.
Ex: this is an example file that can be used as a reference for this program, i want this line to be split (newline) by every 10 words each.

private void jButton27ActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String[] names = jTextArea13.getText().split("\\n");

           var S = names.Split().ToList();
           for (int k = 0; k < S.Count; k++) {
               nam.add(S[k]);
               if ((k%10)==0) { 
                   nam.add("\r\n");       
               }
           }

           jTextArea14.setText(nam);


output:
this is an example file that can be used as
a reference for this program, i want this line to
be split (newline) by every 10 words each.

Any help is appreciated.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
Samantha1154
  • 117
  • 9

3 Answers3

6

I am looking for a way to split my chunk of string every 10 words

A regex with a non-capturing group is a more concise way of achieving that:

str = str.replaceAll("((?:[^\\s]*\\s){9}[^\\s]*)\\s", "$1\n");

The 9 in the above example is just words-1, so if you want that to split every 20 words for instance, change it to 19.

That means your code could become:

jTextArea14.setText(jTextArea13.getText().replaceAll("((?:[^\\s]*\\s){9}[^\\s]*)\\s", "$1\n"));

To me, that's much more readable. Whether it's more readable in your case of course depends on whether users of your codebase are reasonably proficient in regex.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

You were so close.

You were not appending your split words before setting it back into your text box. StringBuilder sb.append(S[k]) will add your split name to a buffer. sb.append(" ") will then add a space. Each line will be of 10 space separated names.

StringBuilder sb = new StringBuilder();
String[] names = jTextArea13.getText().split(" ");


for (int k = 0; k < S.length; k++) {

   sb.append(S[k]).append(" ");
   if (((k+1)%10)==0) { 
      sb.append("\r\n");       
   }
}

At last print it back to your jTextArea using:

jTextArea14.setText(sb.toString());

Just a side note, since sb is StringBuilder, you need to change it to string using toString nethod.

YoManTaMero
  • 391
  • 4
  • 10
  • 2
    give some explanation . . . . .. . . . . . . – Istiaque Hossain Jul 31 '19 at 10:05
  • This isn't quite right. If you append the value and *then* check divisibility by 10 as you're doing here, then you'll always have the first word printed on a line on its own, and then the rest of them split every 10 after that. – Michael Berry Jul 31 '19 at 10:17
0

You can try this as well leveraging the java util

public static final String WHITESPACE = " ";
public static final String LINEBREAK = System.getProperty("line.separator");


 public static String splitString(String text, int wordsPerLine)
  {
    final StringBuilder newText = new StringBuilder();

    final StringTokenizer wordTokenizer = new StringTokenizer(text);
    long wordCount = 1;
    while (wordTokenizer.hasMoreTokens())
    {
        newText.append(wordTokenizer.nextToken());
        if (wordTokenizer.hasMoreTokens())
        {
            if (wordCount++ % wordsPerLine == 0)
            {
                newText.append(LINEBREAK);
            }
            else
            {
                newText.append(WHITESPACE);
            }
        }
    }
    return newText.toString();
   }
Jigar Shah
  • 470
  • 1
  • 6
  • 13