gpt4 book ai didi

java - 如何检测此类字符串中的新行

转载 作者:太空狗 更新时间:2023-10-29 15:37:25 26 4
gpt4 key购买 nike

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

我想在单击按钮时检测运行时的断行我想知道新行出现..因为在 TEXTVIEW 中,我希望输出与此字符串相同,没有任何变化..所以请告诉我一些想法或逻辑或源代码......

最佳答案

中没有换行符或特殊的“换行”字符
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.";


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

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

没有“不可见的\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.

关于java - 如何检测此类字符串中的新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5090494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com