2

I'm really not sure how to best ask this, so hopefully someone can figure out what I'm talking about.

Assume a List that looks something like this:

[
    "Harum beatae autem occaecati. Incidunt quis",
    "eius perferendis itaque dolorum dolores doloribus.",
    "Veniam voluptate mollitia\net aut qui magnam. Ut",
    "quos unde nulla qui. Earum odit dolores non illo illum."
]

I want to split it at the "\n" like so :

[
    "Harum beatae autem occaecati. Incidunt quis",
    "eius perferendis itaque dolorum dolores doloribus.",
    "Veniam voluptate mollitia",
    "et aut qui magnam. Ut",
    "quos unde nulla qui. Earum odit dolores non illo illum."
]
6
  • 2
    What have you tried so far? Commented Aug 7, 2018 at 14:51
  • I cannot differ between input and output. Commented Aug 7, 2018 at 14:52
  • @LuiggiMendoza One of the strings has been split. The output list has five strings instead of four. Commented Aug 7, 2018 at 14:53
  • @LuiggiMendoza, there is just one... Commented Aug 7, 2018 at 14:53
  • Input is four lines with a "\n" halfway through the third, output is five lines split at the newline Commented Aug 7, 2018 at 14:53

3 Answers 3

10

If you are using Java 8 you can use stream and flatMap like this :

List<String> result = list.stream()
        .flatMap(s -> Arrays.stream(s.split("\n")))
        .collect(Collectors.toList());

If not you can use a simple loop like so :

List<String> result = new ArrayList<>();
for (String s : list){
    result.addAll(Arrays.asList(s.split("\n")));
}
Sign up to request clarification or add additional context in comments.

13 Comments

I am actually using Java 8, the first option throws this error: Cannot infer type argument(s) for <R> flatMap(Function<? super T,? extends Stream<? extends R>>)
Second option seems to work (but throws a security warning) but crashes on run
Small Correction : s.split("\n") should be s.split("\\n")
@DanGriffiths this is not possible check the demo of java 8 solution and a demo of simple loop
@zlakad I read before a nice book I would suggest to read it It explain how flatMap and every this how its work with an intelligent way the book is Java 8 Lambdas hope this can help you ;)
|
0

Try this one.

 public List<String> extractNewLineSeparatedStrings(String input) {

      List<String> result = new ArrayList<String>();        
      if (input != null) {
        // split by new line
        String[] arr = input.split("\\n");
        result =  Arrays.asList(arr);
      }
      return result;
  }

1 Comment

You're expecting a string input, I'm starting with a List
0

Adding another way(simple) to achieve, using StringTokenizer

List<String> result = new ArrayList<>();
for (String s : list){
   StringTokenizer st = new StringTokenizer(s,"\n");  
   while(st.hasMoreTokens()) {
   result.add(st.nextToken());
}
}

5 Comments

Interestingly, the code as provided does nothing. If I use \\n instead of \n, it breaks at every instance of 'n'... with or without the slash
Just did a test, changed it to match "%n" and it split at every instance of "n" as well. Oops?
pls send your correct input. i tried my answer in IDE and it is working fine
Right now my input is this: List<String> unformattedText = fontRenderer.listFormattedStringToWidth(inputText, maxWidth - 10); Where inputText is: inputText<String> = "Harum beatae\n autem occaecati."
let me see how you tried in your coding. cause this would surely work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.