Home > Java > javaTutorial > body text

What are the ways to convert java string to array?

王林
Release: 2023-05-05 14:13:06
forward
1818 people have browsed it

1.split()

can split a string based on matching a given regular expression.

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

Syntax structure:

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

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

2.toCharArray()

Convert the characters in the string object into a character array.

Syntax:

public char[] toCharArray()
Copy after login

Parameters

None

Instance

String s=abcd;
char a[]=s.toCharArray();
Copy after login

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

Related labels:
source:yisu.com
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