簡體   English   中英

如何將多行字符串轉換為行保持縮進數組

[英]How to convert multi-line String to array of line keeping indentation

字符串變量中的 C 或 C++ 代碼。 我想將該多行字符串變量轉換為字符串列表,但我想保留縮進以在我的 angular 應用程序中顯示代碼。 這就是我正在做的

String diffContent = "multi line Java c or c++ code";
List<String> lines = IOUtils.readLines(new StringReader(diffContent));

但是我因此而失去了縮進。 有沒有可能的方法來實現這一點。

嘗試根據 "\n" 分割行:

String splitLines[] = diffContent.split("\n");

然后在新行中顯示每個字符串。

嘗試這個

   String diffContent = "multi line Java c or c++ code";
    String str[] = diffContent.split(" ");
        List<String> al = new ArrayList<String>();
        al = Arrays.asList(str);
        for(String s: al){
           System.out.println(s);
        }
       }

}

暫無
暫無

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

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