Java的String的split方法忽略空字符串

编程入门 行业动态 更新时间:2024-10-26 21:23:19
本文介绍了Java的String的split方法忽略空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

今天发生在我的java的行为 String.split()是很奇怪的。

It occured to me today the behavior of java String.split() is very strange.

其实我想要分割字符串AA,BB,CC,DD ,,, EE来阵列由 .split( )这给了我一个String数组 [AA,BB,抄送,DD,,,EE] 长7。

Actually I want to split a string "aa,bb,cc,dd,,,ee" to array by .split(",") that gives me a String array ["aa","bb","cc","dd","","","ee"] of length 7.

但是当我尝试拆分字符串AA,BB,CC,DD ,,,,来排列这给了我一个长度为4的数组只是意味着 [AA,BB,抄送,DD] 拒绝所有一个空白字符串。

But when I try to split a String "aa,bb,cc,dd,,,," to array this gives me a array of length 4 means only ["aa","bb","cc","dd"] rejecting all next blank Strings.

我要那个分裂像AA,BB,CC,DD ,,,,来阵列 [AA的字符串的程序,BB,抄送,DD,,,] 。

I want a procedure that splits a String like "aa,bb,cc,dd,,,," to array ["aa","bb","cc","dd","","",""].

这可能与java.lang.String中的API?先谢谢了。

Is this possible with java.lang.String api? Thanks in advance.

推荐答案

使用String.split(String正则表达式,INT限制) 负限制(例如-1)。

Use String.split(String regex, int limit) with negative limit (e.g. -1).

"aa,bb,cc,dd,,,,".split(",", -1)

在String.split(String正则表达式) 被调用时,它被称为与限制 = 0,这将删除阵列中的所有尾随空字符串(在大多数情况下,见下文)。

When String.split(String regex) is called, it is called with limit = 0, which will remove all trailing empty strings in the array (in most cases, see below).

的实际行为 String.split(字符串正则表达式)还是比较混乱的:

The actual behavior of String.split(String regex) is quite confusing:

  • 分割一个空字符串将导致长度为1的数组的空字符串拆分将总是导致包含空字符串长度1阵列的
  • 拆分,或;;;与正则表达式是,将导致空数组。的非空字符串分割将导致删除阵列中的所有尾随空字符串的
  • Splitting an empty string will result in an array of length 1. Empty string split will always result in length 1 array containing the empty string.
  • Splitting ";" or ";;;" with regex being ";" will result in an empty array. Non-empty string split will result in all trailing empty strings in the array removed.

以上可以观察到至​​少5的Java到Java 8。行为

The behavior above can be observed from at least Java 5 to Java 8.

有是企图去改变在拆分 JDK-6559590 。然而,它很快就在 JDK-8028321 恢复时,它会导致回归在不同的地方。这种变化从不入初始Java 8版本。

There was an attempt to change the behavior to return an empty array when splitting an empty string in JDK-6559590. However, it was soon reverted in JDK-8028321 when it causes regression in various places. The change never makes it into the initial Java 8 release.

更多推荐

Java的String的split方法忽略空字符串

本文发布于:2023-11-04 12:31:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557946.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   空字符串   Java   String   split

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!