lomed
lomed

Reputation: 51

how to split a String with number of character and the character is white space

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 + "";
}

Upvotes: 1

Views: 426

Answers (5)

shmosel
shmosel

Reputation: 50716

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).

Upvotes: 2

shmosel
shmosel

Reputation: 50716

Inspired by kajacx's answer:

public String breakLines(String input, int maxWidth) {
    return input.replaceAll("(\\G.{1," + maxWidth + "})(\\s+|$)", "$1\n"));
}

Upvotes: 0

kajacx
kajacx

Reputation: 12939

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

Upvotes: 1

Narmer
Narmer

Reputation: 1454

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.

Upvotes: 1

Mustafa sabir
Mustafa sabir

Reputation: 4360

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.

Upvotes: 0

Related Questions