这是我非常简单的代码。但是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 :(得分:4)
显然使用“。”正如一个正则表达式是一个错误。最有可能的是"\\."
这是返回一个空数组的原因是因为split会截断尾随分隔符。每个字符都是一个分隔符,因此整个数组被截断为长度为0。
你可以做到
String[] trivia=test.split(".", -1);
这应该返回一个空字符串数组,每个字符加一个。
最有可能的是
String[] trivia=test.split("\\.");
这不会将所有字符视为分隔符,仅处理.
字符。这将截断您可能想要的任何尾随.
。
答案 1 :(得分:0)
我想你想说
trivia=test.split("[.]");
或
trivia=test.split("\\.");
两者都意味着在字符点字符上拆分字符串。