views:

98

answers:

5

I need to split this line string in each line, I need to get the third word(film name) but as you see the delimeter is one big blank character in some cases its small like before the numbers at the end or its big as in front of numbers at front.

I tried using string split with(" ") regex, and also \t but get the out of the bounds error.

400115305   Lionel_Atwill   The_Song_of_Songs_(1933_film)   7587
400115309   Brian_Aherne    A_Night_to_Remember_(1943_film) 7952

Did anyone have the same problem?

+3  A: 

Have you tried to split on all whitespaces like this: line.split("\\s+"). The split method supports regular expressions.

edit: sorry, should be \\s indeed.

The following code

String line = "400115305   Lionel_Atwill   The_Song_of_Songs_(1933_film)   7587";
System.out.println(Arrays.toString(line.split("\\s+")));

returns with [400115305, Lionel_Atwill, The_Song_of_Songs_(1933_film), 7587] on my machine (Java 6 SE).

Marc
Should that read `line.spluit("\\s")`?
Alison
This one will not work. A good one should be "\\ +"
vodkhang
I tested again, \\s will also not work
vodkhang
A: 

This code should work. A good one should be s.split("\\ +");

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String s = "400115305   Lionel_Atwill   The_Song_of_Songs_(1933_film)   7587";
        String [] s2 = s.split("\\ +");
        for (String string : s2) {
            System.out.println("string = " + string);
        }
    }
}
vodkhang
A: 

This code:

String s = "400115305   Lionel_Atwill   The_Song_of_Songs_(1933_film)   7587\r\n"
        + "400115309   Brian_Aherne    A_Night_to_Remember_(1943_film) 7952";

String[] lines = s.split("\\r\\n"); // split lines
for (String line : lines) {
    String[] items = line.split("[\\s\u00A0]+"); // split by whitespace OR  
    System.out.println("Film=" + items[2]);
}

has the following output:

Film=The_Song_of_Songs_(1933_film)
Film=A_Night_to_Remember_(1943_film)
True Soft
A: 

Why you do not use Regular Expression for your need. It's very flexiable and can easy solve your problem. Here is a link may be help you: http://www.regular-expressions.info/java.html

nmquyet
A: 

The solution was str.split("\\t");

London