String text = "test_123
check_1 123
test_1
ASD@#$
qwer #$%^
test 1
this is book";
and turn it into a string array like:
{"test_123","check_1 123","test_1","ASD@#$","qwer #$%^","test 1","this is book"}
String text = "test_123
check_1 123
test_1
ASD@#$
qwer #$%^
test 1
this is book";
and turn it into a string array like:
{"test_123","check_1 123","test_1","ASD@#$","qwer #$%^","test 1","this is book"}
4
Answers
The split function is what will help you to achieve this, you’ll have to identify a pattern, which is a regular expression, that work as the split rule.
From what I see the String text doesn’t have an intuitive pattern
If you want single elements, you can do this:
However, like someone pointed out, there’s no recognizable pattern in what you wanna do. You can try some weird regex inside the split but I doubt it’ll work.
EDIT: If you add n at the end of each block of text/string it will work. Example:
This should do the trick
There are a few ways to achieve this.
You can use the String#split method, which uses a regular expression pattern as the argument.
The following should suffice in identifying the new-line delimiter.
Additionally, you could use the BufferedReader and StringReader classes.
Similarly, you could use the Scanner class.
All of these methods will output the following.
You can use String.split method. Regular expressions can be used to define delimiter. For example: