Java-String-split() method white version

The split() method is explained in detail

effect:

  • split in java is mainly used to separate strings

 Grammar format:

 Parameter details: o(* ̄▽ ̄*)o

 ① The first parameter, regex of String type: indicates the basis for segmentation, or the basis for judgment

 ② The second parameter, limit of int type: the incoming parameter indicates how many arrays to divide 

 Example 1, (only the basis for segmentation, pay attention to " , "):  

public class Demo1 {
    
    private static String s = "Banana,Apple,Grape,Mango,Pear";

    public static void main(String[] args) {

        //因为 split会将 符合分割条件的 字符串 分解成一个个的 小数组  
        //因此需要一个 字符数组 进行接收

        String[] text = s.split(",");

        System.out.println("分割后的结果如下所示:");
        for (int i=0;i<text.length;i++){
            System.out.println(text[i]);
        }
    }

}

In this result the commas are gone

Example 2, (restrictions are divided into several groups, that is, the second parameter is passed in)

public class Demo1 {

    private static String s = "Banana,Apple,Grape,Mango,Pear";

    public static void main(String[] args) {
        
        String[] text = s.split(",",3);

        System.out.println("只分3组的结果如下所示:");
        for (int i=0;i<text.length;i++){
            System.out.println(text[i]);
        }
        }
    }

}

 

The commas in the first two groups are gone, but not in the last group


 Summarize

  • ① From the print results, the first parameter passed in (regex " , ") disappears, so more consideration should be given when using it
  • ② When the second parameter is passed in, only the regex that matches the first two groups (this article is " , ") disappears, and the subsequent ones remain unchanged


ps: It is said that in the study of the class library (headache), but you also need to take more notes, read more and more, and check if you forget. Plus lots of practice. . . Also, thank you for seeing yourself here ^O^

Only when you are very narrow at the beginning can you understand people, and after dozens of steps after revival, you will suddenly see the light! Let's work hard together~ (๑•̀ㅂ•́)و✧

If there are mistakes or bad writing, please point them out~ I will listen carefully

Guess you like

Origin blog.csdn.net/weixin_53353693/article/details/119255908