1

So I am splitting a string by line separators, but I also want to save what line separator was used to use later when putting the string back together. How can I do that?

String data = "Multi\n Line\r Text\n";
List<String> separators = new ArrayList<>();
String lines[] = data.split("\\r?\\n|\\r");
separators.add(???);

I honestly don't know how to capture that. Is it possible with String.Split? Or is there another way that I can go about doing this?

I'm initially getting the string from a File. Is there a way to get and save the line separators that way before converting it into a string?

Dtb49
  • 1,211
  • 2
  • 19
  • 48

2 Answers2

0

You can use StringTokenizer as a starting point:

    String data = "Multi\n Line\r Text\n and now a two-char \r\nline separator";
    StringTokenizer tokenizer = new StringTokenizer(data, "\n\r", true);
    List<String> tokensAndDelimiters = new ArrayList<>();
    while (tokenizer.hasMoreTokens()) {
        tokensAndDelimiters.add(tokenizer.nextToken());
    }
    tokensAndDelimiters.replaceAll((token) -> token.replace("\n", "\\n").replace("\r", "\\r"));
    System.out.println("tokensAndDelimiters = " + tokensAndDelimiters);

The output of the above code is:

tokensAndDelimiters = [Multi, \n,  Line, \r,  Text, \n,  and now a two-char , \r, \n, line separator]

The third StringTokenizer constructor argument indicates to return delimiters as tokens. I'm only replacing the line separator characters so that they print out clearly. One thing to note is that if you have a typical windows line separator ("\r\n"), this will come out as two tokens because StringTokenizer only deals with single character delimiters.

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • Hmm, not sure if I'll be able to use this or not. Do you know if there is a way to do this through the Scanner or through the File class? – Dtb49 Nov 19 '18 at 20:43
0
String data = "Multi\n Line\r Text\n";
List<String> separators = new ArrayList<>(Arrays.asList(data.split("[[^\\r?\\n]&&[^\\r]]+")));
separators.remove(0); // first element is an empty string
String lines[] = data.split("\\r?\\n|\\r");

//reconstruction
for (int i = 0; i < lines.length; i++) {
    System.out.print(lines[i] + StringEscapeUtils.escapeJava(separators.get(i)).trim());
}

Output

Multi\n Line\r Text\n
Kartik
  • 7,677
  • 4
  • 28
  • 50