杰瑞科技汇

Java String转数组有哪些常用方法?

Of course! Converting a Java String to an array is a common task, but the exact method depends on what kind of array you want to create. Here’s a comprehensive guide covering the most common scenarios.

Java String转数组有哪些常用方法?-图1
(图片来源网络,侵删)

Summary: Which Method to Use?

Goal Method to Use Example
Split a string by a delimiter String.split() "a,b,c".split(",") -> ["a", "b", "c"]
Get an array of characters String.toCharArray() "abc".toCharArray() -> ['a', 'b', 'c']
Get an array of individual bytes String.getBytes() "abc".getBytes() -> [97, 98, 99]
Get an array of code points String.codePoints() (returns a stream, convert to array) "a😀c".codePoints() -> [97, 128512, 99]
Convert a comma-separated string String.split() (often followed by Arrays.stream().mapToInt().toArray()) "1,2,3".split(",") -> [1, 2, 3] (as int)

Splitting a String into a String Array

This is the most frequent use case. You want to break a string into parts based on a separator (a delimiter).

Method: String.split(String regex)

This method takes a regular expression as an argument and returns an array of strings.

Important Note: The delimiter is a regular expression. This means characters like , , , , , ^, , , , [, , \ have special meanings. If you want to split on one of these characters, you must escape it with a double backslash (\\).

Example 1: Simple Delimiter (Comma)

String csvString = "apple,banana,cherry,date";
String[] fruits = csvString.split(",");
// Print the array
System.out.println(Arrays.toString(fruits));
// Output: [apple, banana, cherry, date]

Example 2: Handling Multiple Delimiters

Let's split a string that has multiple spaces as delimiters. The regular expression for "one or more whitespace characters" is \\s+.

Java String转数组有哪些常用方法?-图2
(图片来源网络,侵删)
String text = "Java   is  fun";
String[] words = text.split("\\s+"); // Use \\s+ for one or more spaces
System.out.println(Arrays.toString(words));
// Output: [Java, is, fun]

Example 3: Splitting by a Special Character (Dot)

Since is a regex wildcard, we must escape it.

String sentence = "first.second.third";
String[] parts = sentence.split("\\."); // Note the double backslash
System.out.println(Arrays.toString(parts));
// Output: [first, second, third]

Example 4: Limiting the Number of Splits

The split method has an overloaded version: split(String regex, int limit).

  • If limit > 0: The array will have a maximum of limit elements. The last element will contain the rest of the string.
  • If limit < 0: The array can have any number of elements (same as not providing a limit).
  • If limit == 0: The array can have any number of elements, but trailing empty strings are discarded.
String data = "one,two,three,four,five";
String[] limitedParts = data.split(",", 3); // Split into max 3 parts
System.out.println(Arrays.toString(limitedParts));
// Output: [one, two, three,four,five]

Converting a String to a Character Array

If you want each character of the string to be an element in the array.

Method: String.toCharArray()

This is straightforward and does not use regular expressions.

String name = "Alice";
char[] chars = name.toCharArray();
System.out.println(Arrays.toString(chars));
// Output: [A, l, i, c, e]

Converting a String to a Byte Array

This is useful for low-level operations like file I/O or network transmission, where data is handled as bytes.

Method: String.getBytes()

This method converts the string into a sequence of bytes using the platform's default character set. This can lead to data loss if the string contains characters not in the default charset.

For predictable results, it's better to specify a character set (e.g., StandardCharsets.UTF_8).

import java.nio.charset.StandardCharsets;
String message = "Hello, 世界"; // Contains non-ASCII characters
// Using default charset (not recommended for portability)
byte[] bytesDefault = message.getBytes();
System.out.println(Arrays.toString(bytesDefault));
// Output may vary depending on your system's default encoding (e.g., Windows-1252 on some Windows systems)
// Using a specific, standard charset (recommended)
byte[] bytesUTF8 = message.getBytes(StandardCharsets.UTF_8);
System.out.println(Arrays.toString(bytesUTF8));
// Output: [72, 101, 108, 108, 111, 44, 32, -28, -72, -83, -27, -101, -67]

Converting a String to a Primitive Array (e.g., int[])

This is a very common requirement. You have a string like "1,2,3,4" and want an array of integers [1, 2, 3, 4]. This requires a multi-step process.

Method: Combine split(), Arrays.stream(), and mapToInt().toArray()

  1. Split the string into a String[].
  2. Create a stream from the array using Arrays.stream().
  3. Map each String element to an int using mapToInt(Integer::parseInt).
  4. Collect the results into an array using toArray().
import java.util.Arrays;
String numbersStr = "10,25,30,42,55";
// Step 1: Split the string
String[] numberStrings = numbersStr.split(",");
// Step 2-4: Convert to a stream, map to int, and collect to array
int[] numbers = Arrays.stream(numberStrings)
                       .mapToInt(Integer::parseInt) // Or .mapToInt(s -> Integer.parseInt(s))
                       .toArray();
System.out.println(Arrays.toString(numbers));
// Output: [10, 25, 30, 42, 55]

Handling Potential Errors:

If the string contains non-numeric data, Integer.parseInt() will throw a NumberFormatException. You should handle this gracefully, for example, by using a try-catch block or filtering out invalid values.

String badNumbersStr = "10,hello,30,world,55";
int[] safeNumbers = Arrays.stream(badNumbersStr.split(","))
                          .mapToInt(s -> {
                              try {
                                  return Integer.parseInt(s.trim());
                              } catch (NumberFormatException e) {
                                  return 0; // Or some other default value
                              }
                          })
                          .toArray();
System.out.println(Arrays.toString(safeNumbers));
// Output: [10, 0, 30, 0, 55]

Handling Unicode Code Points

Sometimes you need to work with Unicode code points instead of char values, especially for characters outside the Basic Multilingual Plane (like emojis), which can be represented by two char values (a surrogate pair).

Method: String.codePoints()

This method returns an IntStream of Unicode code points. You can then collect this stream into an array.

String emojiString = "I am a 😀";
// Get an array of code points
int[] codePoints = emojiString.codePoints().toArray();
System.out.println(Arrays.toString(codePoints));
// Output: [73, 32, 97, 109, 32, 97, 32, 128512]
// The last element, 128512, is the code point for the 😀 emoji.
// It would take two char values (55357 and 56832) to represent it.
分享:
扫描分享到社交APP
上一篇
下一篇