簡體   English   中英

如何在這種類型的字符串中檢測新行

[英]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.";

我想在運行時檢測斷線,當我點擊按鈕我想知道新線來..因為在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.";


當我點擊按鈕我想知道新線來..

您可以通過執行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.

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

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

如果您的GUI在此之前突破了界限,則需要以某種方式禁用自動換行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM