gpt4 book ai didi

java - 为什么使用 split (".") 返回一个空数组?

转载 作者:行者123 更新时间:2023-11-29 07:38:46 26 4
gpt4 key购买 nike

这是我非常简单的代码。但是 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,这意味着零位置没有值。谁能解释为什么?请帮忙。

最佳答案

显然使用“.”因为正则表达式是一个错误。很可能应该是 "\\."

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

你可以做到

String[] trivia=test.split(".", -1);

这应该返回一个空字符串数组,每个字符加一个。

很可能是你想要的

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

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

关于java - 为什么使用 split (".") 返回一个空数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com