Skip to content Skip to sidebar Skip to footer

How To Detect New Line In This Type Of String

String quote = 'Now is the time for all good '+ 'men to come to the aid of their country.'; I want to detect break line in run time when I click on button I want to

Solution 1:

There are no new-lines or special "break-line" characters in

Stringquote="Now is the time for all good "+
               "men to come to the aid of their country.";

The statement is entirely equivalent to

Stringquote="Now is the time for all good men to come to the aid of their country.";

When I click on button I want to know new line comes..

You can find out where each new-line is by doing yourText.indexOf("\n"). The snippet below for instance, prints 29.

String quote = "Now is the time for all good \n" +
               "men to come to the aid of their country.";

System.out.println(quote.indexOf('\n'));

ya i want to sperate some words by invisble "\n"

There are no "invisible \n". In source code, you have to use \n or something equivalent. Java does not support heredoc or anything equivalent to that.

To break up a string into multiple lines, you could do something like:

String quote = "Now is the time for all good " +
               "men to come to the aid of their country.";

quote = quote.substring(0, 29) + "\n" + quote.substring(29);

System.out.println(quote);

which prints:

Now is the time for all good 
men to come to the aid of their country.

Solution 2:

The '\n' character specifies a line break.

"Now is the time for all good\nmen to come to the aid of their country."

If your GUI is breaking the line before this point you need to disable word wrapping somehow.

Post a Comment for "How To Detect New Line In This Type Of String"