4

Imagine i have the following String:

hoi
hoe 
gaat 
het

i get this String from a file.

how can i make this in to a sting array like this:

String[] hallo = {
        "hoi","hoe","gaat","het"        
        };

What would be the most simple way to achieve this?

Mister Verleg
  • 3,709
  • 5
  • 43
  • 59
  • 3
    If you get the text from a file consider reading it line by line and adding the strings to a list. That list can then easily be converted to an array if that's what you really need (often you better keep using lists). – Thomas Feb 11 '16 at 12:55
  • 4
    or do `List lines = Files.readAllLines(Paths.get("myfile.txt"));` in the first place. – zapl Feb 11 '16 at 13:00
  • 1
    I want to know the continuation to the story! "Hoi! Hoe gaat het met je? ..." – Noobs DeSroobs Feb 11 '16 at 13:51
  • @zapl, i find your way very convenient – Pankaj Nimgade Feb 11 '16 at 14:45

6 Answers6

6

You can use split().

String s[] = input.split("\n"); // "\n" if it is only a new-line. "\r\n" if you use windows OS.
dryairship
  • 5,842
  • 2
  • 29
  • 53
6

This works on any system.

String[] array = str.split(System.getProperty("line.separator"));
anaxin
  • 710
  • 2
  • 7
  • 16
  • 2
    Yes, actually better than mine. – dryairship Feb 11 '16 at 12:56
  • Not really, the line separator in files is not necessarily the system one. http://stackoverflow.com/questions/454908/split-java-string-by-new-line solves that. – zapl Feb 11 '16 at 13:01
5

From Java 8, there are some built-in methods for that :

Files.readAllLines(Path)

it gives a List<String>. You can then convert it to an array if you really need to.

Files.lines(Path)

it gives a Stream<String>. You can then handle each String without having to load the whole file in memory.

Arnaud Denoyelle
  • 28,245
  • 14
  • 82
  • 137
3

Just another approach. Split based on anything which isn't between [a-zA-Z]. Works across platforms :

public static void main(String[] args) throws IOException, InterruptedException {
    String s = "asda\r\nasdsa";
    System.out.println(Arrays.toString(s.split("[^a-zA-Z]+")));
}

O/P :

[asda, asdsa]
TheLostMind
  • 35,468
  • 12
  • 66
  • 99
0
    try {
        File file = new File("file.txt");
        Path path = file.toPath();
        List<String> strings = Files.readAllLines(path); // this is available on **java 8**
     // List<String> strings = FileUtils.readLines(file); // this can be used with **commons-io-2.4 library** 
        for (String s : strings) {
            System.out.println(s);
        }
        String[] string_array = new String[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            string_array[i] = strings.get(i);
        }
        System.out.println(Arrays.deepToString(string_array));

    } catch (Exception e) {
        e.printStackTrace();
    }
Pankaj Nimgade
  • 4,429
  • 3
  • 19
  • 29
0

If the input is in one entire string, you can do this to split the string into array of strings :

String[] hallo = input.split("[ ]+");
Sujit
  • 91
  • 1
  • 3