Home > Java > JavaBase > How to convert string to array in java

How to convert string to array in java

王林
Release: 2020-07-09 14:14:17
Original
18079 people have browsed it

The way Java converts a string into an array is through the split method, which can split the string according to matching a given regular expression. Specific usage: [String[] arr = str.split(",")], which means splitting the string with commas and storing it in an array.

How to convert string to array in java

#We can achieve this through the split method.

(Recommended tutorial: java introductory program)

Method introduction:

split() method can be split according to matching the given regular expression Divide into strings.

Note: Escape characters such as ., | and * must be added with \\. Multiple delimiters, you can use | as a hyphen.

Grammar structure:

public String[] split(String regex, int limit)
Copy after login

(Video tutorial recommendation: java video tutorial)

Parameter introduction:

regex -- Regular Expression delimiter.

limit -- The number of divided copies.

Example:

// 字符串转数组  java.lang.String
String str = "0,1,2,3,4,5";
String[] arr = str.split(","); // 用,分割
System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4, 5]
Copy after login

The above is the detailed content of How to convert string to array in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template