【问题标题】:Java how to get certain word from array [closed]Java如何从数组中获取某些单词[关闭]
【发布时间】:2015-12-18 08:36:09
【问题描述】:

我有一个存储的数组

Array[0] = "Information of student, Class Allocated: 2B (13), Class Opposite: 1B (12)"
Array[1] = "Information of student, Class Allocated: 1B (12), Class Opposite: 2B (13)"
Array[2] = "Information of student, Class Allocated: 1A (10), Class Opposite: 2A (11)"

我需要Class Allocated:和Class Opposite的信息。因此我需要获得 2B 和 13 但存储在不同的变量中。哪个可以

For Array[0]
Floor = 2B
ClassNo = 13
Opposite = 1B
OppositeClass = 12

谁能告诉我如何获得它?非常感谢您的帮助。

【问题讨论】:

  • 你能说清楚一点吗?
  • 到目前为止您尝试过什么?
  • 谷歌“java正则表达式”
  • 首先为Array这一行定义自己的数据模型,然后提供一个方法来解析一行到模型,例如:pastebin.com/H7i8GNg5

标签: java


【解决方案1】:

在String中使用split方法。

假设你有标准格式。

String [] commasplit = Array[0].split(",");

然后

String [] allocated = commasplit[1].split(" : ");
String allocatedValue = allocated[1] //2B (13)

给定代码,用方法参数写一个泛型方法,这样你就不会写多次了。

【讨论】:

  • @new to Java - 按空格分割:)
  • 也可以拆分 2B 和 13 吗?例如字符串 floor = 2B; int 房间 = 13
  • 这应该通过模式匹配来完成,而不是通过简单的拆分
猜你喜欢
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多