day08-常用类-String-的分割方法_split
This commit is contained in:
19
day08/src/com/inmind/string01/Demo07.java
Normal file
19
day08/src/com/inmind/string01/Demo07.java
Normal 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());
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user