如何在这种类型的字符串中检测新行

时间:2011-02-23 11:51:36

标签: java android

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

我想在运行时检测断线,当我点击按钮时我想知道新的线路来了.. 因为在TEXTVIEW中我想要输出相同的字符串没有任何变化..所以告诉我一些想法或逻辑或源代码....

2 个答案:

答案 0 :(得分:9)

中没有新行或特殊的“断行”字符
String quote = "Now is the time for all good "+
               "men to come to the aid of their country.";

该陈述完全等同于

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


  

当我点击按钮时,我想知道新线路来了......

您可以通过yourText.indexOf("\n")找出每个换行符的位置。例如,下面的代码段打印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'));


  

你想通过隐形“\ n”来拼写一些单词

没有“隐形\n”。在源代码中,您必须使用\n或类似的东西。 Java不支持heredoc或与此类似的任何内容。

要将字符串分成多行,您可以执行以下操作:

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

打印:

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

答案 1 :(得分:3)

'\ n'字符指定换行符。

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

如果您的GUI在此之前突破了界限,则需要以某种方式禁用自动换行。