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

This commit is contained in:
2026-05-16 14:44:11 +08:00
parent 464b18be9d
commit 76461a9b21

View File

@@ -0,0 +1,19 @@
package com.inmind.string01;
/*
常用类_String的分割方法_split
public String[] split(String regex) 将此字符串按照给定的regex规则拆分为字符串数组
*/
public class Demo07 {
public static void main(String[] args) {
String str = "张三,18,28,上海";
//按,分割,获取对应的数据。注意:按什么分割,该字符串就不会出现
String[] strs = str.split(",");
System.out.println(strs.length);//4
//将一段字符串转为学生对象
Student s = new Student(strs[0], Integer.parseInt(strs[2]));
System.out.println(s.getName());
System.out.println(s.getAge());
}
}