-2

How can I split the following string to an array of {"George", "Washington"};

String arr = "George\nWashington";
ivesingh
  • 888
  • 3
  • 12
  • 30
  • 2
    Oh my, googling splitting a string in Java returns a ton of results, some of which are from Stack Overflow. Perhaps you had a specific question in mind, such as how to split on the `\n`. If so, make that clear in your question, otherwise it might be closed quickly because it looks like you haven't tried anything (and someone might link to whathaveyoutried.com) – Ray Toal Aug 17 '13 at 05:12
  • @Ray I think he might have attempted it using split but the possibility is that he wasn't using the escape sequence in the right manner. – A M Aug 17 '13 at 05:17
  • Exactly! I'm sure that was all it was. It was meant to be a polite suggestion. I was afraid people would jump on the question, and was hoping that by giving this suggestion the OP would avoid the downvotes and the close. It is certainly not obvious how to specify control characters in strings, especially in Java, so it was a good question. It just needed to be made more clear because as it stands now, it got -2 votes, unfortunately. – Ray Toal Aug 17 '13 at 06:57

2 Answers2

4
String[] array = arr.split("\\n");
A M
  • 448
  • 5
  • 11
3

Try this:

String arr[] = "George\nWashington".split("\\n"));
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136