简体   繁体   中英

How do I split this String (Java)?

I want to split the String String line = "20E WED 01PM 0E"; so that I have variables for each "part," where the parts would look something like:

int fDegree = 20;
String fDegreeEW = "E";
String day = "WED";
int time = 01;   \\ '1' is fine too
String amPm = "PM";
int sDegree = 0;
String sDegreeEW = "E";

I was thinking of first splitting the String line at spaces and then doing regex on each part to get the data, but I couldn't get that to work. What would be the nicest way to split a String like this into a bunch of different variables? Note: This is not the only String I want to check. The first "20" could also be "1" or "165", for example, so the words and numbers do not necessarily have a set length. But the pattern of number, word, space, word, space, number, word, space, number, word will always be true.

Edit: I tried:

String line = "20E WED 01PM 0E";
String[] splitArray = line.split(" ");
Scanner scanner = new Scanner(splitArray[0]);
int fDegree = scanner.nextInt();

but that threw an InputMismatchException . Can I use scanner here or no?

This regex will do it:

(\d+)(\w+)\s+(\w+)\s+(\d+)(\w+)\s+(\d+)(\w+)

See regex101 for demo (look at "match information" pane on the right).

Here's how to code:

Pattern p = Pattern.compile("(\\d+)(\\w+)\\s+(\\w+)\\s+(\\d+)(\\w+)\\s+(\\d+)(\\w+)");

String line = "20E WED 01PM 0E";
Matcher m = p.matcher(line);
if (! m.matches())
    throw new IllegalArgumentException("Bad input: " + line);
int    fDegree   = Integer.parseInt(m.group(1));
String fDegreeEW = m.group(2);
String day       = m.group(3);
int    time      = Integer.parseInt(m.group(4));
String amPm      = m.group(5);
int    sDegree   = Integer.parseInt(m.group(6));
String sDegreeEW = m.group(7);

The pattern variable p can be reused for every line.

Johnny is right about the Scanner, but you should also consider how the data will be entered. It would be a lot easier to split this information if you gave the pattern a set length.

For example, if you have the line entered as "020 E WED 13 000 E", then all you need to do is split based on the token " " (space). You would then be given a three digit degree, your cardinal direction, the day, 1:00pm on a 24 hour clock (13), another three digit degree (I'm assuming that you'll never go above 360), and another cardinal direction.

If you need to save this information in a data structure (like an array) for later, you can either use line.split() or StringTokenizer.

You don't use scanner like that. Scanner is usually for letting user input a value to the system. If you want to print out all value i suggest this:

String line = "20E WED 01PM 0E";
String[] splitted = line.split(" ");
for(int i = 0; i < splitted.length(); i++){
System.out.println(splitted[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM