1. Overview
1.概述
String is probably one of the most commonly used types in Java.
String可能是Java中最常用的类型之一。
In this tutorial, we’ll explore how to convert a String into a String array (String[]).
在本教程中,我们将探讨如何将一个字符串转换为字符串数组(String[])。
2. Introduction to the Problem
2.对问题的介绍
Converting a string to a string array could have two scenarios:
将一个字符串转换成一个字符串数组可能有两种情况。
- converting the string to a singleton array (an array with only one single element)
- breaking the string into elements of an array following a particular rule
Case 1 is relatively easy to understand. For example, if we have a string “baeldung”, we want to convert it to String[]{ “baeldung” }. In other words, the converted array has only one element, which is the input string itself.
情况1相对容易理解。例如,如果我们有一个字符串“baeldung”,我们想把它转换成String[]{ “baeldung” }。换句话说,转换后的数组只有一个元素,那就是输入的字符串本身。
For case 2, we need to break the input string into pieces. However, how the result should be is entirely dependent on the requirement. For example, if we expect each element in the final array contains two adjacent characters from the input string, given “baeldung”, we’ll have String[]{ “ba”, “el”, “du”, “ng” }. Later, we’ll see more examples.
对于情况2,我们需要将输入的字符串分成几块。然而,结果应该是怎样的,完全取决于需求。例如,如果我们期望最终数组中的每个元素都包含输入字符串中的两个相邻的字符,给定“baeldung”,我们会有 String[]{ “ba”, “el”, “du”, “ng” }。以后,我们会看到更多的例子。
In this tutorial, we’ll take this string as the input:
在本教程中,我们将以这个字符串作为输入。
String INPUT = "Hi there, nice to meet you!";
Of course, we’ll cover both conversion scenarios. Further, for simplicity, we’ll use unit test assertions to verify whether our solutions work as expected.
当然,我们将涵盖这两种转换情况。此外,为了简单起见,我们将使用单元测试断言来验证我们的解决方案是否按预期工作。
3. Converting to a Singleton Array
3.转换为单子数组
As the input string will be the only element in the target array, we can simply initialize an array using the input string to solve the problem:
由于输入字符串将是目标数组中的唯一元素,我们可以简单地使用输入字符串初始化一个数组来解决这个问题。
String[] myArray = new String[] { INPUT };
assertArrayEquals(new String[] { "Hi there, nice to meet you!" }, myArray);
Then, if we run the test, it passes.
然后,如果我们运行这个测试,它就会通过。
4. Converting the Input String Into Elements in an Array
4.将输入的字符串转换为数组中的元素
Now, let’s see how to break the input string into segments.
现在,让我们看看如何将输入的字符串分成几段。
4.1. Using the String‘s split() Method
4.1.使用字符串的split()方法
We often need to work with input strings in specific patterns. In this case, we can break the inputs into a String array using regular expressions or regex. Java’s String class provides the split() method to do this job.
我们经常需要以特定模式处理输入字符串。在这种情况下,我们可以使用正则表达式或regex将输入分解成一个String数组。Java的String类提供了split()方法来完成这项工作。
Next, we’ll address splitting our input example into an array following a few different requirements.
接下来,我们将讨论按照一些不同的要求将我们的输入例子分割成一个数组。
First, let’s say we want to split the input sentence into an array of clauses. To solve this problem, we can split the input string by punctuation marks:
首先,假设我们想把输入的句子分割成一个子句阵列。为了解决这个问题,我们可以通过标点符号来分割输入的字符串。
String[] myArray = INPUT.split("[-,.!;?]\\s*" );
assertArrayEquals(new String[] { "Hi there", "nice to meet you" }, myArray);
It’s worth mentioning that when we need a regex’s character class to contain a dash character, we can put it at the very beginning.
值得一提的是,当我们需要一个regex的字符类包含一个破折号字符时,我们可以把它放在最开始。
The test above shows that the input string is broken into two clauses in an array.
上面的测试显示,输入的字符串被分解成数组中的两个子句。
Next, let’s extract all words from the same input string into an array of words. This is also a common problem we may face in the real world.
接下来,让我们把同一输入字符串中的所有单词提取成一个单词数组。这也是我们在现实世界中可能面临的一个普遍问题。
To get the word array, we can split the input by non-word characters (\W+):
为了得到字数组,我们可以通过非字字符(\W+)分割输入。
String[] myArray = INPUT.split("\\W+");
assertArrayEquals(new String[] { "Hi", "there", "nice", "to", "meet", "you" }, myArray);
Finally, let’s break the input string into characters:
最后,让我们将输入的字符串分解成字符。
String[] myArray = INPUT.split("");
assertArrayEquals(new String[] {
"H", "i", " ", "t", "h", "e", "r", "e", ",", " ",
"n", "i", "c", "e", " ", "t", "o", " ", "m", "e", "e", "t", " ", "y", "o", "u", "!"
}, myArray);
As the code above shows, we use an empty string (zero width) as the regex. Every character, including the space in the input string, is extracted as an element of the target array.
如上面的代码所示,我们使用一个空字符串(零宽度)作为regex。每个字符,包括输入字符串中的空格,都被提取为目标数组的一个元素。
We should note the String.toCharArray() converts the input to an array too. However, the target array is a char array (char[]) instead of a String array (String[]).
我们应该注意到String.toCharArray()也将输入转换为一个数组。然而,目标数组是一个char数组(char[]),而不是String数组(String[])。
The three examples used the String.split() method to convert the input string to different string arrays. Some popular libraries, such as Guava and Apache Commons, also provide enhanced string split functionalities. We’ve talked about that in another article in detail.
这三个例子使用String.split()方法将输入字符串转换为不同的字符串数组。一些流行的库,如Guava和Apache Commons,也提供了增强的字符串分割功能。我们已经在另一篇文章中详细地谈到了这一点。
Furthermore, we have many other articles to discuss how to solve different concrete splitting problems.
此外,我们还有许多其他文章来讨论如何解决不同的具体拆分问题。
4.2. Special Parsing Requirements
4.2.特殊解析要求
Sometimes we must follow a particular rule to split the input. An example can quickly clarify it. Let’s say we have this input string:
有时我们必须遵循一个特定的规则来分割输入。一个例子可以迅速说明问题。比方说,我们有这样一个输入字符串。
String FLIGHT_INPUT = "20221018LH720FRAPEK";
And we expect to get this array as a result:
而我们希望得到这个数组作为结果。
{ "20221018", "LH720", "FRA", "PEK" }
Well, at the first glance, this conversion logic looks obscure. However, if we list the definition of the input string, we’ll see why the array above is expected:
嗯,乍一看,这个转换逻辑看起来很晦涩。然而,如果我们列出输入字符串的定义,我们就会发现为什么上面的数组是预期的。
[date][Flight number][Airport from][Airport to]
- date: YYYY-MM-DD; length:8
- Flight number; length: variable
- Airport From: IATA airport code, length:3
- Airport To: IATA airport code, length:3
As we can see, sometimes we need to parse the input string following a pretty special rule. In that case, we need to analyze the requirement and implement a parser:
我们可以看到,有时我们需要按照一个相当特殊的规则来解析输入字符串。在这种情况下,我们需要分析需求并实现一个解析器。
String dateStr = FLIGHT_INPUT.substring(0, 8);
String flightNo = FLIGHT_INPUT.substring(8, FLIGHT_INPUT.length() - 6);
int airportStart = dateStr.length() + flightNo.length();
String from = FLIGHT_INPUT.substring(airportStart, airportStart + 3);
String to = FLIGHT_INPUT.substring(airportStart + 3);
String[] myArray = new String[] { dateStr, flightNo, from, to };
assertArrayEquals(new String[] { "20221018", "LH720", "FRA", "PEK" }, myArray);
As the code above shows, we’ve used the substring() method to build a parser and processed the flight input correctly.
正如上面的代码所示,我们已经使用substring()方法建立了一个解析器,并正确处理了飞行输入。
5. Conclusion
5.总结
In this article, we’ve learned how to convert a String to a String array in Java.
在这篇文章中,我们已经学习了如何在Java中把String转换为String数组。
Simply put, converting a string to a singleton array is pretty straightforward. If we need to break the given string into segments, we can turn to the String.split() method. However, if we need to break the input following a particular rule, we may want to analyze the input format carefully and implement a parser to solve the problem.
简单地说,将一个字符串转换为一个单子数组是非常直接的。如果我们需要将给定的字符串分解成片段,我们可以求助于String.split()方法。然而,如果我们需要按照特定的规则来分解输入,我们可能要仔细分析输入格式,并实现一个解析器来解决这个问题。
As always, the full code used in the article is available over on GitHub.
一如既往,文章中所使用的完整代码可在GitHub上获得。