Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么使用split(";)返回空数组?_Java_Regex - Fatal编程技术网

Java 为什么使用split(";)返回空数组?

Java 为什么使用split(";)返回空数组?,java,regex,Java,Regex,这是我非常简单的代码。但是split()方法不起作用 String test="Germany (/?d??rm?ni/; German: Deutschland), officially the Federal Republic of Germany (German: Bundesrepublik Deutschland, pronounced [?b?nd?s?epu?bli?k ?d??t?lant] (13px )), is a federal parliamentary republ

这是我非常简单的代码。但是
split()
方法不起作用

 String test="Germany (/?d??rm?ni/; German: Deutschland), officially the Federal Republic of Germany (German: Bundesrepublik Deutschland, pronounced [?b?nd?s?epu?bli?k ?d??t?lant] (13px )), is a federal parliamentary republic in western-central Europe. It consists of 16 constituent states, which retain limited sovereignty, and covers an area of 357,021 square kilometres (137,847 sq mi) with a largely temperate seasonal climate. Its capital and largest city is Berlin. Germany is a major economic and political power and traditionally a leader in many cultural, theoretical and technical fields.With 80.7 million inhabitants, Germany is the most populous member state in the European Union. After the United States, it is also the second most popular migration destination in the world. Germany has the world's fourth-largest economy by nominal GDP and the fifth-largest by PPP. As a global leader in several industrial and technological sectors, it is both the world's third-largest exporter and third-largest importer of goods. It is a developed country with a very high standard of living, featuring comprehensive social security that includes the world's oldest universal health care system. Known for its rich cultural and political history, Germany has been the home of many influential philosophers, artists, musicians, cineasts, entrepreneurs, scientists and inventors. Germany was a founding member of the European Communities in 1957, which became the European Union in 1993. It is part of the Schengen Area, and has been a member of the Eurozone since 1999. Germany is a member of the United Nations, NATO, the G8, the G20, the OECD and the Council of Europe. Various Germanic tribes have occupied what is now northern Germany and southern Scandinavia since classical antiquity. A region named Germania was documented by the Romans before AD 100. During the Migration Period that coincided with the decline of the Roman Empire, the Germanic tribes expanded southward and established kingdoms throughout much of Europe. Beginning in the 10th century, German territories formed a central part of the Holy Roman Empire. During the 16th century, northern German regions became the centre of the Protestant Reformation. The rise of Pan-Germanism inside the German Confederation, which had been occupied by France during the Napoleonic Wars, resulted in the unification of most of the German states in 1871 into the Prussian-dominated German Empire. As a result of the military defeat in World War I, and the German Revolution of 1918–1919, the Empire was replaced by the parliamentary Weimar Republic. The establishment of the Third Reich, or Nazi Regime, in 1933 eventually led to World War II and the Holocaust. In 1945, the remnants of the Nazi regime surrendered to the Allied Powers. Over the next few years, Germany lost more of its territory and was divided by the victors into Allied occupation zones, and evolved into two states, East Germany and West Germany. On 3 October 1990, the country was reunified, regaining full sovereignty about six months later.";
 String[] trivia=null;
 trivia=test.split(".");
 System.out.println(trivia[0]);
java.lang.ArrayIndexOutOfBoundsException,这意味着在位置0处没有值。 有人能解释为什么吗?请帮助。

显然,将“.”用作正则表达式是一个错误。最有可能是
“\\”

此操作返回空数组的原因是split截断了尾随分隔符。每个字符都是分隔符,因此整个数组被截断为长度0

你能行

String[] trivia=test.split(".", -1);
这将返回一个空字符串数组,每个字符一个加一个

很可能你的意图是

String[] trivia=test.split("\\.");
这不会将所有字符视为分隔符,仅将
字符视为分隔符。这将截断任何尾随的
,这可能是您想要的。

显然,使用“.”作为正则表达式是一个错误。最有可能是
“\\”

此操作返回空数组的原因是split截断了尾随分隔符。每个字符都是分隔符,因此整个数组被截断为长度0

你能行

String[] trivia=test.split(".", -1);
这将返回一个空字符串数组,每个字符一个加一个

很可能你的意图是

String[] trivia=test.split("\\.");

这不会将所有字符视为分隔符,仅将
字符视为分隔符。这将截断任何尾随的
,这可能是您想要的。

我想您是想说的

trivia=test.split("[.]");


两者都意味着在一个文字点字符上拆分一个字符串。

我想你的意思是

trivia=test.split("[.]");


这两种方法都意味着在文字点字符上拆分字符串。

您正在按字符串中所有可能的字符进行拆分。可能您希望/需要使用
\\.
按点分割。
分割功能正在按设计工作。您可能需要考虑这样一个事实,
split()
的参数是一个正则表达式,因此“.”的含义可能与您认为的不同。祝你好运。还有一件事:任何时候,当你认为你在广泛使用的软件发布的界面中发现了一个bug时,你可能会想再想想。一次又一次。一次又一次。然后用细齿梳子仔细检查,找出你不明白的地方。虽然在广泛使用的软件(如Java)中确实存在bug,但您发现bug的可能性要比您的理解需要更新的可能性小得多。祝你好运。你正在按字符串中所有可能的字符进行拆分。可能您希望/需要使用
\\.
按点分割。
分割功能正在按设计工作。您可能需要考虑这样一个事实,
split()
的参数是一个正则表达式,因此“.”的含义可能与您认为的不同。祝你好运。还有一件事:任何时候,当你认为你在广泛使用的软件发布的界面中发现了一个bug时,你可能会想再想想。一次又一次。一次又一次。然后用细齿梳子仔细检查,找出你不明白的地方。虽然在广泛使用的软件(如Java)中确实存在bug,但您发现bug的可能性要比您的理解需要更新的可能性小得多。祝你好运。