2

I have a long string and I split it by space character, in some parts there are more than one space so a space gets to become a string. I have created a for loop and trying to identify those space strings to skip them but unfortunately they cannot be found by the code that I'm using.

public static void main( String[] args ){  


    String s = "Monday Tuesday Wednesday 8:00       8:15       8:30       8:45       9:00       9:15       9:30       9:45       10:00 IT Service Mgt. F1.2 wkFeb2-wkMar23, wkApr13-wkMay11 IT Service Mgt. B180 wkFeb2-wkMar23, wkApr13-wkMay11 Strategic IT Mgt. F1.2 wkFeb2-wkMar23, wkApr13-wkMay11 10:15 10:30 10:45 11:00 Strategic IT Mgt. B145 wkFeb2-wkMar23, wkApr13-wkMay11   11:15   11:30   11:45   12:00     12:15     12:30     12:45     13:00 IT Service Mgt. B145 wkFeb2-wkMar23, wkApr13-wkMay11 IT Service Mgt. E7 wkFeb2-wkMar23, wkApr13-wkMay11   13:15   13:30   13:45   14:00   Sec. Penetration Testing B251 wkFeb2-wkMar23, wkApr13-wkMay11 Ent. Storage Sys. C129 wkFeb2-wkMar23, wkApr13-wkMay11 14:15   14:30   14:45   15:00   Ent. Storage Sys. C129 wkFeb2-wkMar23, wkApr13-wkMay11 15:15   15:30   15:45   16:00   Sec. Penetration Testing C136 wkFeb2-wkMar23, wkApr13-wkMay11 Ent. Storage Sys. C134x wkFeb2-wkMar23, wkApr13-wkMay11 16:15   16:30   16:45   17:00     17:15     17:30     17:45    ";
    String[] parts = s.split(" ");
    String space = " ";
    for(int i=0; i<parts.length;i++)
    {   

        if(parts[i].charAt(0) == ' ')
        {
            System.out.println("askdfhlghlaksjdgsdasasfasfasdfasd");
        }
        if(parts[i].compareTo(" ")==0){
            System.out.print("asdfghgfdsaASDFGHGFDSASDFGHJKJHGFDSASDFGHJKJHGTFRD");
        }
        System.out.print(parts[i]);
    }
} 
rlemon
  • 17,518
  • 14
  • 92
  • 123
Tad Lithuania
  • 105
  • 1
  • 9

2 Answers2

3

Use a regular expression for your split like s.split("[ ]+").

[ ]+ means match against space occurring at least once.

If you want to match space or tab occurring at least once, you can use [\t ]+.

If you want to skip the spaces at the beginning or end of string, use trim.

Here is a summary of Java regular expression constructs.

ruben2020
  • 1,549
  • 14
  • 24
  • I tried but it doesn't help.. – Tad Lithuania Feb 19 '15 at 14:01
  • Are you trying to get an array of "Monday", "Tuesday", "Wednesday", "8:00", "8:15" and so on? – ruben2020 Feb 19 '15 at 14:05
  • no there is more involved in it but only think is that I am trying to identify a space character because its either a space character or a text starts after e.c 9:00 or 10:00. All I need is the data after it. – Tad Lithuania Feb 19 '15 at 14:11
  • If spaces are part of your normal column content, then it is better to use something else as the delimiter like tabs. Or we could consider putting column texts with spaces inside quotation marks like "New Year's Day". – ruben2020 Feb 19 '15 at 14:14
  • If you're generating the text file from Excel or Libre Calc, it is possible to select which delimiter to use. – ruben2020 Feb 19 '15 at 14:21
  • I'm getting the data from a timetable website using JSoup but this is the only thing that I'm stuck on right now. – Tad Lithuania Feb 19 '15 at 14:35
  • If you want to skip the spaces at the beginning or end of string, use [`trim`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim%28%29). – ruben2020 Feb 19 '15 at 14:42
0

Use the following regular expression:

String parts = s.trim().split("\\s+");

Output of parts:

[Monday, Tuesday, Wednesday, 8:00, 8:15, 8:30, 8:45, 9:00, ...]

Now, the above will allow tabs and other white space characters though. To eliminate them all, you can do:

String parts = s.trim().split(" +");
Ascalonian
  • 14,409
  • 18
  • 71
  • 103