简体   繁体   English

在数组列表Java中分割字符串

[英]splitting strings in an array-list Java

I have an ArrayList consisting of dates. 我有一个由日期组成的ArrayList All of the dates are in this format: 所有日期均采用以下格式:

"day month time timezone year". “日月时间时区年”。 An example would look like this, Wed Aug 10:15:17 AST 2016 . 例如, AST 2016年8月10日星期三15:15:17

I need to split the string at the second ":". 我需要在第二个“:”处分割字符串。 Here is my code to do that. 这是我的代码。

int dateSize = date.size();
int i = 0;
ArrayList<String> splitDate = new ArrayList<String>();
splitDate.clear();
while(i < dateSize) {
    String[] splitString = date.get(i).split(":" + 1);
    splitDate.add(splitString[0]);
    Log.i("info", String.valueOf(splitDate));
    i++;
}

The ArrayList consists about 50 items. ArrayList包含约50个项目。 At first, this code works. 首先,此代码有效。 It prints this to the log, 它将其打印到日志中,

Wed Sep 14 23:30 , which is what I want. 我想要的是9月14日星期三23:30

But then, at random, it starts to print this, 但随后,它开始随机打印

Wed Sep 14 23:30, Wed Sep 14 23:24:36 AST 2016, Wed Sep 14 23, Wed Sep 14 22, Wed Sep 14 22, Tue Sep 13 22, Tue Sep 13 22, Tue Sep 13 22:07:33 AST 2016, Mon Sep 12 20 AST 2016年9月14日星期三23:30、9月14日星期三23:24:36、9月14日星期三,9月14日星期三,9月14日星期三,9月13日星期二,9月13日星期二22日,9月13日星期二22:07: 33 AST 2016,9月12日星期一

Sometimes it splits the second ":", sometimes it splits the first ":", sometimes it doesn't split anything. 有时会拆分第二个“:”,有时会拆分第一个“:”,有时不拆分任何内容。 I cannot figure out why this is happening. 我不知道为什么会这样。 IF I change my code to this, 如果我将代码更改为此,

String[] splitString = date.get(i).split(":");
splitDate.add(splitString[0]);

It works flawlessly. 它完美地工作。 Everything gets printed to the log like this, Wed Aug 10 , no exceptions. 一切都将像这样打印在日志上, 8月10星期三 ,无一例外。

Is my approach correct? 我的方法正确吗?

Ok, first of all this isn't what you want 好吧,首先这不是您想要的

String[] splitString = date.get(i).split(":" + 1);

This is splitting on the String ":1" 这在字符串“:1”上拆分

What you want to do is split on ":" and then get the first two entries in the array and concatenate them 您想要做的是在“:”上分割,然后获取数组中的前两个条目并将它们连接起来

String[] splitString = date.get(i).split(":");
splitDate.add(splitString[0] + ":" + splitString[1]);

As one of the comments says you problem is this line 就像其中一条评论说你的问题是这条线

String[] splitString = date.get(i).split(":" + 1);

Essentially you're going to be passing ":1" as the argument to split(). 本质上,您将传递“:1”作为sp​​lit()的参数。 Therefore you'll end up selecting severything up tot he first instance of ":1", or the whole string if ":1" isn't present. 因此,您将最终选择“:1”的第一个实例中的所有内容,如果不存在“:1”,则选择整个字符串。 For example: 例如:

  • Wed Aug 10 :15:17 AST 2016 will give Wed Aug 10 AST 2016年8月10星期三 :15:17将给8月10日星期三
  • Wed Aug 10:25 :17 AST 2016 will give Wed Aug 10:25 八月10:25:17星期三 AST 2016将为八月10:25星期三
  • Wed Aug 10:25:25 AST 2016 will give Wed Aug 10:25:25 AST 2016 AST 2016年8月10日25:25:25将提供AST 2016年8月10日25:25:25

If you know thee format of the string then I'd just do: 如果您知道字符串的格式,那么我就这样做:

String dateStr = date.substring(0, 13)

If you don't know the format of the strings (eg they've been typed in by a user who may have typoed some) then I'd check them with a regex. 如果您不知道字符串的格式(例如,用户可能打错了某些字符,则键入了它们),那么我将使用正则表达式对其进行检查。

If you have the an array element of strings in the format "Wed Aug 10:15:17 AST 2016" and you split/delimit by ":", you will end up with the following array of strings: 如果您有一个字符串数组元素,其格式为“ Wed Aug 10:15:17 AST 2016”,并且用“:”分隔/定界,则将得到以下字符串数组:

[0] Wed Aug 10
[1] 15
[2] 17 AST 2016

If you look at the above array, you cannot accomplish your desired output of "Wed Aug 10 15:17" with just splitting the string and then adding the components (ie: You will miss the '17' in the above example, as it falls in element [2]). 如果看上面的数组,仅通过拆分字符串然后添加组件就无法完成所需的“ Wed Aug 10 15:17”输出(即:在上面的示例中您将错过“ 17”,因为它属于元素[2])。

I am not a Java dev, so the syntax may be a bit off, but if you want to generate "Wed Aug 10 15:17", then I believe you are looking for this: 我不是Java开发人员,因此语法可能有点不正确,但是如果您想生成“ Wed Aug 10 15:17”,那么我相信您正在寻找:

String[] splitString = date.get(i).split(":");\\remove "1" from your delimiter
String tempString = splitString[2].substring(0,1);
splitDate.add(splitString[0] + " " + splitString[1] + ":" + tempString);

In the above snippet, the tempString line is getting the first two characters from the split string array element [2]. 在上面的代码片段中,tempString行从拆分的字符串数组元素[2]中获取前两个字符。 You then add the first two array elements with the tempString (ie: 17) and get your output. 然后,您将前两个数组元素与tempString相加(即:17)并获得输出。

Edit: I just wanted to mention that if you have control over the input of date strings, I would advise using a different delimiter. 编辑:我只是想提一下,如果您可以控制日期字符串的输入,我建议您使用其他定界符。 Using ":" causes the issue that the delimiter also exists as a valid, non-delimiter, character in the string. 使用“:”会导致定界符在字符串中也作为有效的非定界符存在的问题。 The above code snippet should work presuming the input is not controlled by you/your method. 假设您/您的方法未控制输入,则上面的代码段应该可以工作。

A bit more complicated way using replaceFirst method. 使用replaceFirst方法要复杂replaceFirst

ArrayList<String> splitDate = new ArrayList<String>();
for (String dateItem : date) {
    String output = dateItem.replaceFirst("^(\\w{3}\\s\\w{3}\\s\\d{2}:\\d{2}):\\d{2}\\s\\w+\\s\\d+$", "$1");
    splitDate.add(output);
    Log.i("info", output);  
}

You can always use the java.util.StringTokenizer class. 您始终可以使用java.util.StringTokenizer类。 Considering date is the ArrayList containing dates in String format, the following lines of code will add the date in the format required. 考虑到date是包含String格式dateArrayList ,以下代码行将以所需格式添加日期。

StringTokenizer dateString = new StringTokenizer(date.get(i), ":");
splitDate.add(dateString.nextToken() + ":" + dateString.nextToken());

Simple enough! 很简单!

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

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