TheGPWorx
TheGPWorx

Reputation: 887

Separating a string that has no whitespaces in JAVA

I am having problems with separating string characters. These are the raw strings that I have

"MH 02:30pm-04:00pm TF 08:30am-10:00am"
"MTWS 07:00 am-09:00 am, A 08:30 am-11:00 am"
"WS 01:00pm-05:00pm F 11:30am-01:00pm"

These strings are database values from a table column named "schedule". I only have read privileges so I can't alter data, and the database is huge making it difficult to change the format of the schedule column.

My purpose is to extract the class schedule from the string returning the day(MTWHFSA), start time and end time. What I have done so far is use StringTokenizer. I have not been successful since as you can see, the string that the database returns varies in format. It doesn't always return the same format. So I've thought to remove all whitespaces so that all will have the same format. Example:

"MH02:30pm-04:00pmTF08:30am-10:00am"

Please, can anyone help me with how to extract the day(MTWHFSA), start time and end time from the string above?

Here's the sample code:

String sched = "MTWHFS 02:30pm-04:00am W 08:30am-10:00am";
StringTokenizer token = new StringTokenizer(sched,",");
    while(token.hasMoreTokens()){
        StringTokenizer anotherToken = new StringTokenizer(token.nextToken(),"\\ ?\\-?");
        while(anotherToken.hasMoreTokens()){
            System.out.println(anotherToken.nextToken());
        }
    }

This outputs correctly:

MTWHFS
02:30pm
04:00am
W
08:30am
10:00am

But if the string is like this:

String sched = "MTWHFS 02:30 pm-04:00 am W 08:30 am-10:00 am";

It's output becomes:

MTWHFS
02:30
pm
04:00
am
W
08:30
am
10:00
am

The output varies when the string format is different, that's why I removed the whitespaces so that the string format will be the same.

But when the string has no white spaces like this:

String sched = "MTWHFS02:30pm-04:00amW08:30am-10:00am";

It outputs this which is no what I wanted.

MTWHFS02:30pm
04:00amW08:30am
10:00am

Can anyone help me with this so that this string:

String sched = "MTWHFS02:30pm-04:00amW08:30am-10:00am";

will output:

MTWHFS
02:30pm
04:00am
W
08:30am
10:00am

Thanks.

Upvotes: 1

Views: 176

Answers (2)

Bernhard Barker
Bernhard Barker

Reputation: 55609

This is another way:

String regex = "(?<=am|pm),?-?|(?<=[A-Z])(?=[0-9])";
System.out.println(Arrays.toString(str.replaceAll(" ", "").split(regex)));

For "MTWHFS 02:30 pm-04:00 am W 08:30 am-10:00 am" it prints:

[MTWHFS, 02:30pm, 04:00am, W, 08:30am, 10:00am]

Explanation:

It may be a bit difficult to understand if you haven't played around with split and probably regular expression a bit already.

Firstly, remove all spaces with replaceAll.

Then it's on to the regular expression.

(?<=am|pm) checks that the previous characters are either am or pm, then you also include an optional , and - so these are consumed in the split.

Alternatively, the previous character must be between A and Z ((?<=[A-Z])) and the next character must be between 0 and 9 ((?=[0-9])).

Example:

MH02:30pm-04:00pm
  ^ here the previous character is "H" and the next character is "0",
     thus it fulfils the second condition, it splits on a string of length 0

MH02:30pm-04:00pm
         ^ here the previous characters are "pm",
            thus it fulfils the first condition, it splits on the "-"

Upvotes: 0

cyon
cyon

Reputation: 9538

You could use regular expressions. The benefit of using a regex would be that it also checks for you whether the input has the expected format.

This regex should match a single schedule entry :

([A-Z]+)([0-9]{2}:[0-9]{2}(?:am|pm))-([0-9]{2}:[0-9]{2}(?:am|pm))

The capture groups in the regex give you the day, start and end times. You can use it in Java like this:

// the input string which may contain spaces
String s = "MH02:30pm-04:00pmTF08:30am-10:00am"; 
s = s.replaceAll("\\s+", ""); //remove the spaces
Pattern rg = Pattern.compile("([A-Z]+)([0-9]{2}:[0-9]{2}(?:am|pm))-([0-9]{2}:[0-9]{2}(?:am|pm))");
final Matcher matcher = rg.matcher(s);
while(matcher.find()) {
    //find all the groups
    for(int i=0;i<=matcher.groupCount();i++) {
        System.out.println(matcher.group(i));
    }
}

Upvotes: 3

Related Questions