【问题标题】:Java: Split a string into separate string and integer variablesJava:将字符串拆分为单独的字符串和整数变量
【发布时间】:2016-11-21 04:14:50
【问题描述】:

我一直在寻找答案,但我没有找到对我有很大帮助的答案(有人可能知道指向我的地方)。

我想将一个字符串拆分为一个字符串变量和两个整数变量。我想知道是否有任何方法可以使用我发现的字符串拆分方法并通过其他方式将其用于 int 变量?

代码:

import java.util.Scanner;

public class StringInputStream {
   public static void main (String [] args) {
      Scanner inSS = null;
      String userInput = "Jan 12 1992";
      inSS = new Scanner(userInput);

      String userMonth = "";
      int userDate = 0;
      int userYear = 0;

      // Can modify anything below here
      String[] temp = userInput.split(" ", 2); // "1" means stop splitting after one space
      userMonth = temp[0];
      userDate = temp[1];
      userYear = temp[2];
      // Can modify anything above here

      System.out.println("Month: " + userMonth);
      System.out.println("Date: " + userDate);
      System.out.println("Year: " + userYear);

      return;
   }
}

示例输出:

Month: Jan
Date: 12
Year: 1992

【问题讨论】:

标签: java string split int


【解决方案1】:

基于您的 cmets,并使用 Scanner 进行编码。我想你想要

userMonth = inSS.next();
userDate = inSS.nextInt();
userYear = inSS.nextInt();

但如果您的作业不需要 Scanner,那么您当然可以消除它并使用类似的东西

String userInput = "Jan 12 1992";
String[] tokens = userInput.split("\\s+"); // <-- split on one or more spaces
String userMonth = tokens[0];
int userDate = Integer.parseInt(tokens[1]);
int userYear = Integer.parseInt(tokens[2]);

【讨论】:

  • 所以使用下一个方法将继续并跳过我声明的任何值?
  • @Aramza 我不确定你在问什么,你有inSS = new Scanner(userInput); 这意味着你的Scanner 正在读取你预定义的Stringnext() 读取 String (Jan),nextInt 读取 12,第三个 nextInt 读取 1992。在第二个版本中,我跳过 Scanner 并使用 String.split
【解决方案2】:

我不想使用大锤来破解 :-) 但根据您的应用程序在提供的输入方面所需的灵活性,您可以使用解析器来解决您的问题。如果您将下面的语法放入ANTLR,您可以解析输入字符串并从中提取语义元素。您甚至可以处理不同的输入排列,例如Jan 1999 121999 Mar 12 等。

grammar SimpleMixed;
fragment A:('a'|'A');
fragment B:('b'|'B');
fragment C:('c'|'C');
fragment D:('d'|'D');
fragment E:('e'|'E');
fragment F:('f'|'F');
fragment G:('g'|'G');
fragment H:('h'|'H');
fragment I:('i'|'I');
fragment J:('j'|'J');
fragment K:('k'|'K');
fragment L:('l'|'L');
fragment M:('m'|'M');
fragment N:('n'|'N');
fragment O:('o'|'O');
fragment P:('p'|'P');
fragment Q:('q'|'Q');
fragment R:('r'|'R');
fragment S:('s'|'S');
fragment T:('t'|'T');
fragment U:('u'|'U');
fragment V:('v'|'V');
fragment W:('w'|'W');
fragment X:('x'|'X');
fragment Y:('y'|'Y');
fragment Z:('z'|'Z');

s: userinput EOF;
userinput: month date year
    | month year date
    | year month date
    | year date month
    | date year month
    | date month year;

month: Month;
date: Date;
year: Year;
Month: J A N |  F E B | M A R | A P R | M A Y | J U N | J U L | A U G | S E P      |
O C T | N O V | D E C;

Date: [1-9][0-2]?;

Year: [1-9][0-9][0-9][0-9];


WS  :  [ \t\r\n]+ -> skip;

假设用户提供输入jan 1999 12。在使用 Antlr 从上面的语法生成的解析器对其进行解析之后,您将获得下面的解析树,您可以从中轻松提取语义元素,基于这些元素您可以推断数据类型并创建适当的对象(例如 int 表示年份和日期, 和字符串为月份)。

【讨论】:

    【解决方案3】:

    好像你在处理日期字符串,最好使用特定的库。

    Java 8

    import java.time.format.DateTimeFormatter;
    import java.time.temporal.*;
    
    TemporalAccessor d = DateTimeFormatter.ofPattern("MMM DD yyyy").parse("Jan 12 1992")
    d.get(ChronoField.YEAR); // 1992
    d.get(ChronoField.MONTH_OF_YEAR); // 1
    d.get(ChronoField.DAY_OF_MONTH); // 12
    

    Java 8 之前 (java.text.SimpleDateFormat)

    Date d = new SimpleDateFormat("MMM DD yyyy").parse("Jan 12 1992");
    Calendar c = Calendar.getInstance();
    c.setTime(d);
    c.get(Calendar.YEAR); // 1992
    c.get(Calendar.MONTH); // 0 - yeah, it starts from 0, embarrassingly
    c.get(Calendar.DAY_OF_MONTH); // 12
    

    或者使用 JodaTime

    DateTimeFormat.forPattern("MMM DD yyyy").parseDateTime("Jan 12 1992");
    

    【讨论】:

    • 此答案仅适用于安装的 Java 版本为 1.8+ 的情况。 @LukeLee
    • 一年中的月份仍然需要是一个字符串
    • OP 正在处理日期时间,我们不要让问题复杂化并且不要使用正确的日期时间库。
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2013-05-23
    相关资源
    最近更新 更多