3

Here is a function which take a long String and return a string divided in paragraph.

The problem is that k is empty. Why split() function doesn't work?

private String ConvertSentenceToParaGraph(String sen) {
    String nS = "";
    String k[] = sen.split(".");

    for (int i = 0; i < k.length - 1; i++) {
        nS = nS + k[i] + ".";
        Double ran = Math.floor((Math.random() * 2) + 4);

        if (i > 0 && i % ran == 0) {
            nS = nS + "\n\n";
        }
    }
    return nS;
}
Tiny
  • 27,221
  • 105
  • 339
  • 599
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

5 Answers5

5

String.split(String regex) takes a regular expression. A dot . means 'every character'. You must escape it \\. if you want to split on the dot character.

4

split expects a regular expression, and "." is a regular expression for "any character". If you want to split on each . character, you need to escape it:

String k[] = sen.split("\\.");
assylias
  • 321,522
  • 82
  • 660
  • 783
3

split() method takes a regex. And . is a meta-character, which matches any character except newline. You need to escape it. Use:

String k[] = sen.split("\\.");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

Change:

sen.split(".");

To:

sen.split("\\.");
barak manos
  • 29,648
  • 10
  • 62
  • 114
1

You need to escape the dot, if you want to split on a dot:

String k[] = sen.split("\\.");

A . splits on the regex ., which means any character.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Rachit
  • 3,173
  • 3
  • 28
  • 45