如何在每個\n
字符處拆分該字符串,並用;
字符替換,最後將它們放入數組中。在c中剪切字符串#
之後,如果數組中的行長度超過60個字符,則再次分割,只是在char 60之前的最後一個空格處。然後在第二部分仍然長於60時重複?
我的代碼是:
var testString = "Lorem Ipsum is simply dummy \n text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, \nwhen an unknown printer took a galley of \n type and scrambled \n it to make a type specimen";
const int maxLength = 60;
string[] lines = testString.Replace("\n", ";").Split(';');
foreach (string line in lines)
{
if (line.Length > maxLength)
{
string[] tooLongLine = line.Split(' ');
}
}
結果:
Lorem存有簡直是虛擬;
印刷和排版行業的文字。 Lorem Ipsum已從
自從16世紀以來的行業標準虛擬文本;
當一臺未知的打印機拿走一個廚房的時候;
type and scrambled;
它製作一個型號的樣本;
你知道你可以只分割'\ n'而不是先做替換。 – juharr
是的,但我需要用\ n替換\ n字符; –
我很困惑..輸出不是你所期望的嗎? –