简体   繁体   English

如何拆分此字符串(Java)?

[英]How do I split this String (Java)?

I want to split the String String line = "20E WED 01PM 0E"; 我想分割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. 我当时想先在空格处分割String line ,然后在每个部分上进行regex以获取数据,但我无法使其正常工作。 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. 例如,第一个“ 20”也可以是“ 1”或“ 165”,因此单词和数字不一定具有设置的长度。 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 . 但这引发了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). 有关演示,请参见regex101 (请查看右侧的 “匹配信息”窗格)。

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. 模式变量p可以在每行中重复使用。

Johnny is right about the Scanner, but you should also consider how the data will be entered. Johnny对Scanner的看法是正确的,但您还应该考虑如何输入数据。 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). 例如,如果您输入的行是“ 020 E WED 13 000 E”,那么您要做的就是根据标记“”(空格)进行拆分。 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. 然后,您将获得一个三位数的学位,您的基本方向,星期几,24小时制(13)的1:00 pm,另一个三位数的学位(我假设您永远不会超过360度),以及另一个基本方向。

If you need to save this information in a data structure (like an array) for later, you can either use line.split() or StringTokenizer. 如果您需要将此信息保存在数据结构(如数组)中以备后用,则可以使用line.split()或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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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