java以空格为分隔标记取子字符串问题
通过out.readLine()取得txt文件一行字符串s="user1123AliceSeller1"时,怎样取其中子字符串“user1”,“123”,"Alice",...
通过out.readLine()取得txt文件一行字符串 s="user1 123 Alice Seller 1" 时,怎样取其中子字符串“user1” ,“123”, "Alice" ,“Seller”, “1” 分别赋值给其对应变量 id , password, name, job, level
展开
2个回答
展开全部
字符串之间空格有几个?
public static void main(String[]args){
String s = "user1 123 Alice Seller 1";
String[] arrays = s.split(" ");
String id = arrays[0];
String password = arrays[1];
String name = arrays[2];
String job = arrays[3];
String level = arrays[4];
System.out.println("id: " +id);
System.out.println("password: " +password);
System.out.println("name: " +name);
System.out.println("job: " +job);
System.out.println("level: " +level);
}
public static void main(String[]args){
String s = "user1 123 Alice Seller 1";
String[] arrays = s.split(" ");
String id = arrays[0];
String password = arrays[1];
String name = arrays[2];
String job = arrays[3];
String level = arrays[4];
System.out.println("id: " +id);
System.out.println("password: " +password);
System.out.println("name: " +name);
System.out.println("job: " +job);
System.out.println("level: " +level);
}
追问
原来是正则表达式拆分,split()参数中空格数竟然有严格规定,难怪之前取不了数据
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询