简体   繁体   English

如何将多行字符串转换为行保持缩进数组

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

C or C++ code in String variable.字符串变量中的 C 或 C++ 代码。 I want to convert that multi line String variable into List of String, but i want to preserve the indentation to display the code in my angular application.我想将该多行字符串变量转换为字符串列表,但我想保留缩进以在我的 angular 应用程序中显示代码。 This is what i am doing这就是我正在做的

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

But i am loosing indentation by this.但是我因此而失去了缩进。 Is there any possible way to achieve this.有没有可能的方法来实现这一点。

Try splitting the lines on the basis of "\n":尝试根据 "\n" 分割行:

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

Then display each String in new line.然后在新行中显示每个字符串。

try this尝试这个

   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