简体   繁体   English

Java中的String.split()是否保证返回有序数组?

[英]Is String.split() in Java guaranteed to return an ordered array?

I'm writing an application where I'm receiving a message that is a sequence of tag1=value1|tag2=value2|tag3=value3 etc. I want to split it around the | 我正在编写一个应用程序,我收到的消息是tag1 = value1 | tag2 = value2 | tag3 = value3等序列。我想将它拆分为| delimiter. 分隔符。

Having read the Javadoc there is nothing to say that the String.split(String regex) method is guaranteed to maintain the original order of the message. 读完Javadoc之后,没有什么可以说String.split(String regex)方法可以保证保持消息的原始顺序。 I've played about and it seems ok, but I'd rather not commit to this approach if I'm going to get caught out later. 我已经玩过了,看起来还不错,但如果我以后会被抓出来,我宁可不承诺这种做法。

So, does anybody know if there are any situations where the order of array elements returned by split() can be altered from the original String? 那么,有没有人知道是否有任何情况下split()返回的数组元素的顺序可以从原始的String改变? Or can anyone point out any documentation that says the order is guaranteed to be maintained? 或者任何人都可以指出任何说明保证订单得到维护的文件?

(Apologies if this is a dupe, but I can't find a similar question on the site.) (如果这是一个骗局,请道歉,但我在网站上找不到类似的问题。)

The javadoc says: javadoc说:

The substrings in the array are in the order in which they occur in this string. 数组中的子串按它们在此字符串中出现的顺序排列。

EDIT : the above is for the two-argument split(String regex, int limit) version, but the single-argument version 编辑 :上面是两个参数split(String regex, int limit)版本,但是单参数版本

works as if by invoking the two-argument split method with the given expression and a limit argument of zero. 就像通过调用给定表达式和limit参数为零的双参数split方法一样工作。

I'm seeing The substrings in the array are in the order in which they occur in this string. 我看到The substrings in the array are in the order in which they occur in this string. in http://docs.oracle.com/javase/7/docs/api/java/lang/String.html ... http://docs.oracle.com/javase/7/docs/api/java/lang/String.html中 ...

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

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