简体   繁体   中英

How to convert multi-line String to array of line keeping indentation

C or C++ code in String variable. 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. 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":

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);
        }
       }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM