2016-07-26 54 views
0

我如何轉換字符串中的格式化字符串我如何如何正常字符串轉換成多行字符串

以下字符串有

"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us"

我必須轉換以上字符串像這樣

"It was the best of times, it was the worst of times,\\n" + 
" it was the age of wisdom, it was the age of foolishness,\\n" + 
" it was the epoch of belief, it was the epoch of incredulity,\\n" + 
" it was the season of Light, it was the season of Darkness,\\n" + 
" it was the spring of hope, it was the winter of despair,\\n" + 
" we had everything before us, we had nothing before us"; 
+0

HM,因爲你沒有說明一種編程語言 - 只是基礎知識:大多數語言提供了一個'分裂()'命令 - 或者使用'replace()'...將正常換行符(\ n)替換爲'「+ \ n」' – Najzero

+0

這通常是通過文本編輯器完成的,大多數高級的支持regexp查找/替換和宏。 –

+0

@Najzero謝謝,我用.replace(「\ n」,「\\ n」);但沒有正確啓動 –

回答

0

只需在字符串中添加一個轉義序列並使用\r\n而不是+。另請參見:https://msdn.microsoft.com/en-us/library/h21280bw.aspx

"It was the best of times, it was the worst of times,\r\n 
    it was the age of wisdom, it was the age of foolishness,\r\n 
    it was the epoch of belief, it was the epoch of incredulity,\r\n 
    it was the season of Light, it was the season of Darkness,\r\n 
    it was the spring of hope, it was the winter of despair,\r\n 
    we had everything before us, we had nothing before us" 

或使用Environment.NewLine物業像

"It was the best of times, it was the worst of times," + Environment.NewLine 
+ " it was the age of wisdom, it was the age of foolishness," + Environment.NewLine 
+ " it was the epoch of belief, it was the epoch of incredulity," + Environment.NewLine 
+ " it was the season of Light, it was the season of Darkness," + Environment.NewLine 
+ " it was the spring of hope, it was the winter of despair," + Environment.NewLine 
+ " we had everything before us, we had nothing before us" 
+0

感謝您的回覆,但即時通訊使用java和jeson字符串 –

相關問題