有个文件里面内容
格式:
2008-11-05 01:54:03 264 10.10.142.158 - - - PROXIED.....
2008-11-05 01:54:03 327 10.10.142.158 - - - 
2008-11-05 01:54:03 356 10.10.142.197 - - - 
2008-11-05 01:54:03 328 10.10.142.180 - - - PROXIED "none" 
2008-11-05 01:54:03 3 10.10.142.181 - - - PROXIED "none" 
现在我要取出 时间:2008-11-05 01:54:03
           耗时:264 
           IP: 264 10.10.142.158
上面这种格式现在我用的是截取方法  碰到空格不好处理
System.out.println(s.substring(20,s.indexOf(" ")+13));
当遇到耗时为个位数字的时候 就把IP的第一个数字截取出来了
类似:
356
328 
3 1查了一下资料  会有什么半角问题
 我现在是想问一下 还有什么好方法获取?

解决方案 »

  1.   


    String time = s.substring(0,19);
    String r = s.substring(20).trim();
    int index = r.indexOf(" ");
    String lang = r.substring(0,index);
    r = r.substring(index).trim();
    int index = r.indexOf(" ");
    String ip = r.substring(0,index);
      

  2.   

    我还是用了截取的办法
       String time = s.substring(0,20).trim();
             
       String s1 = s.substring(20,s.length()).trim();
       String wtime = s1.substring(0,s1.indexOf(" "));
       String s2 = s1.substring(s1.indexOf(" ")+1,s1.length()).trim();
       String ip = s2.substring(0,s2.indexOf(" "));为什么非的首位空格去掉才可以截取
    把里面的原理讲一下