开发者

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 know new line comes.. because in TEXTVIEW i want output same as like this String nothing change.. so tell me some idea or logic or source code of this开发者_如何转开发....


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

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

The statement is entirely equivalent to

String quote = "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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜