將字串”#name:tiger;#age:21;#birthday:1995/11/11 00:00:00;”轉換成Student物件
構建student用來封裝
public class Student { private String name; private String age; private String birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } } 測試類 public class Problems { public static void main(String[] args){ String str="#name:tiger;#age:21;#birthday:1995/11/11 00:00:00;"; String[] info=str.split(";"); Student student=new Student(); student.setName(info[0].replace("#name:", "")); student.setAge(info[1].replace("#age:", "")); student.setBirthday(info[2].replace("#birthday:", "")); System.out.println("name:"+student.getName()); System.out.println("age:"+student.getAge()); System.out.println("birthday:"+student.getBirthday()); } } 顯示結果: name:tiger age:21 birthday:1995/11/11 00:00:00