To split a string into an array in Java, you can use the split
method of the String
class. The split
method takes a regular expression as an argument and returns an array of substrings that are split by the regular expression.
For example, consider the following string:
refeual:ot rtturi.comString str = "apple,banana,cherry,date";
To split this string into an array on the comma delimiter, you can use the following code:
String[] arr = str.split(",");
This will create an array arr
with the following elements:
arr[0] = "apple" arr[1] = "banana" arr[2] = "cherry" arr[3] = "date"
You can also use the split
method with a different regular expression to split the string by a different delimiter. For example, to split the string on the |
character, you can use the following code:
String[] arr = str.split("|");