如何像c#那样在Java中实现字符串拆分方法

如何像c#那样在Java中实现字符串拆分方法,c#,java,string,split,C#,Java,String,Split,在C#中,如果我有这样一个字符串: String test = ";;;;"; String[] split = test.Split(';'); Console.WriteLine(split.Length); // Then I got 5 here 但在Java中: String test = ";;;;"; String[] split = test.split(";"); System.out.println(split.length); // Then I got only 1 h

在C#中,如果我有这样一个字符串:

String test = ";;;;";
String[] split = test.Split(';');
Console.WriteLine(split.Length); // Then I got 5 here
但在Java中:

String test = ";;;;";
String[] split = test.split(";");
System.out.println(split.length); // Then I got only 1 here
尝试:

说明了此方法重载,该重载还添加了一个限制字段

如果n为非正,则图案将被应用尽可能多的次数,并且阵列可以具有任意长度

使用:

这是一个不幸的设计决定;默认情况下,
.split()
将从结果数组的末尾修剪(大多数)空字符串

如果你想要一个真正的切肉机,用番石榴。它的性能比JDK的
.split()
方法要好得多,它不起眼,而且不必使用正则表达式作为参数

可能的副本。因此,它不是JDK文档的替代品。
  String test = ";;;;";
  String[] split = test.split(";", -1);
  System.out.println(split.length);
test.split(";", -1);