day08-常用类-String-的分割方法_split

This commit is contained in:
2026-07-22 14:21:15 +08:00
parent 8d7660b26f
commit cc88d133a2
+25
View File
@@ -0,0 +1,25 @@
package com.inmind.string01;
/*
public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
*/
public class Demo08 {
public static void main(String[] args) {
//定义一个字符串,按照,切割,得到对应的数据
String str = "11,22,33,44,55";
String[] strArr = str.split(",");
System.out.println(strArr);//[Ljava.lang.String;@4eec7777
for (int i = 0; i < strArr.length; i++) {
System.out.println(strArr[i]);
}
System.out.println("--------------------");
//一般会对特定含义的数据进行切割(String---->Student对象)
String str1 = "张三,18,上海,12,男";
String[] studentArr = str1.split(",");
System.out.println("姓名:"+studentArr[0]);
System.out.println("年龄:"+studentArr[1]);
System.out.println("地址:"+studentArr[2]);
System.out.println("学号:"+studentArr[3]);
System.out.println("性别:"+studentArr[4]);
}
}