Java

java中怎么用split切割字符串数组

小亿
114
2024-03-05 16:07:54
栏目: 编程语言

在Java中,可以使用split方法来切割字符串数组。下面是一个示例:

public class Main {
    public static void main(String[] args) {
        String str = "apple,orange,banana,grape";
        String[] fruits = str.split(",");
        
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

在上面的示例中,我们首先定义一个包含多个水果名称的字符串,并使用逗号作为分隔符将其切割成一个字符串数组。然后,我们使用for循环遍历数组中的每个元素,并将其打印出来。结果将会输出:

apple
orange
banana
grape

1
看了该问题的人还看了