[Split method of character string]

package cn.itcast.day08.demo02;
/*
Methods for splitting strings:
public String[] split(String regex): According to the rules of the parameters, the string is cut into several parts.

Precautions:
The parameter sent by the split method is actually a regular expression, which will be learned in the future
Pay attention today: If you split according to the English period ".", you must write "\\." (two backslashes)
 */
public class Demo05StringSplit {
    public static void main(String[] args) {
        String str1 = "aaa,bbb,ccc";
        String[] array1 = str1.split(",");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }
        System.out.println("====================");

        String str2 = "aaa bbb ccc";
        String[] array2 = str2.split(" ");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
        System.out.println("====================");
        String str3 = "XXX.YYY.ZZZ";
        String[] array3 = str3.split("\\.");
        System.out.println(array3.length);//0
        for (int i = 0; i < array3.length; i++) {
            System.out.println(array3[i]);
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_48114733/article/details/123301543