Home  >  Article  >  Java  >  What are the ways to convert java string to array?

What are the ways to convert java string to array?

王林
王林forward
2023-05-05 14:13:061815browse

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)

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]

2.toCharArray()

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

Syntax:

public char[] toCharArray()

Parameters

None

Instance

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

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete