1

I want to split a String into lines with a maximum number of characters per line, while splitting only on whitespace.

example :

if my String is good morning , today is monday good morning , today is monday number of characters : 11

the output should be

good
morning , 
today is 
monday good
morning , 
today is 
monday

this is my code for two rows

public String skipRowLettreNumbre(String lettre) {
        String ret = lettre + "\n";
        if (lettre.length() > 36) {
        String after50 = lettre.substring(36);
        for (int i = 0; i < after50.length(); i++) {
            if (after50.substring(i, i + 1).equals(" ")) {
                String part1 = lettre.substring(0, i + 36);
                String part2 = lettre.substring(i + 36, lettre.length());
                ret = part1 + "\n   " + part2;
                break;
            }
        }
    }
    return ret + "";
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
lomed
  • 51
  • 7

5 Answers5

2

Try this:

public String breakLines(String input, int maxWidth) {
    StringBuilder sb = new StringBuilder();
    int charCount = 0;
    for(String word : input.split("\\s")) {
        if(charCount > 0) {
            if(charCount + word.length() + 1 > maxWidth) {
                charCount = 0;
                sb.append('\n');
            } else {
                charCount++;
                sb.append(' ');
            }
        }
        charCount += word.length();
        sb.append(word);
    }
    return sb.toString();
}

Note: This method will replace all whitespace characters with single spaces (or line breaks).

shmosel
  • 49,289
  • 6
  • 73
  • 138
1

Not sure what you want to achieve, but wouldn't a str.trim().split(" ") do the job?

String str = "good morning , today is monday good morning , today is monday ";

String[] arrayStr = str.trim().split(" ");

If you want the number of occurences just take int a = arrayStr.length -1

EDIT: Still can't get exactly what you want. As I understood you want to split the string with fixed characters numbers AND not split words (such as scho\n ol). If so you can have a look at this question. Is not an easy task.

Community
  • 1
  • 1
Narmer
  • 1,414
  • 7
  • 16
1

OK, after some googling i found out solution using rexeg matcher:

String text = "good morning , today is monday good morning , today is monday ";
String patternString = "(.{1,11}\\s+)";

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

int count = 0;
ArrayList<String> list = new ArrayList<>();
while (matcher.find()) {
    count++;
    String match = matcher.group().trim();
    System.out.println(match);
    list.add(match);
}
String[] result = list.toArray(new String[count]);

This basically search text for 1 to 11 any characters (.{1,11}) followed by at least one whitespace (\\s+) and breaks the text into these parts.

Please beware that the input string must end with a whitespace in order for this to work, so add an extra space to the end of your string when you use this, or change \\s+ to (\\s+|$) (at least one whitespace, or the end of string).

Also, here are tutorials i have followed to write this:

Java rexeg, Java regex quantifiers and Matcher tutorial

Output:

good
morning ,
today is
monday good
morning ,
today is
monday
kajacx
  • 12,361
  • 5
  • 43
  • 70
0

Your question seems quite unclear, but with what i make of it, you want to split your String based on white space character. Its simple:-

String string="good morning , today is monday good morning , today is monday";
String[] arr=string.split(" "); 

Here arr is your String array that hold all your Strings.

Mustafa sabir
  • 4,130
  • 1
  • 19
  • 28
  • but i want to limit number of caracters in any String in array – lomed Jun 25 '14 at 08:59
  • What do you mean by limit number of characters in any String? do you want to shorten the String, for example reduce the String to include only 9 characters `good morning , today is monday good morning , today`? – Mustafa sabir Jun 25 '14 at 09:14
  • cuting string in parts any parts has 9 caracters and dont cat world – lomed Jun 25 '14 at 09:47
  • OP is asking for line wrapping, and i think that putting each word on separate line isn't wery much good line wrapping. – kajacx Jun 25 '14 at 10:22
  • Ohk , sorry I could not make that out from the problem statement, I think then in that case your solution would work. – Mustafa sabir Jun 25 '14 at 10:27
0

Inspired by kajacx's answer:

public String breakLines(String input, int maxWidth) {
    return input.replaceAll("(\\G.{1," + maxWidth + "})(\\s+|$)", "$1\n"));
}
Community
  • 1
  • 1
shmosel
  • 49,289
  • 6
  • 73
  • 138