Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spliting a string after 7th space in java

Tags:

java

split

I have a

String data =
-rw-rw-r--+ 1 aime1 svrtech 83338 Apr 2 10:26 sat.log -rw-rw-r--+ 1 aime1 svrtech 2435 Apr 2 10:48 MAT.log -rw-rw-r--+ 1 aime1 svrtech 3470 Apr 2 08:35 ant_build.log 

I want to split this as following and want to store in ArrayList

-rw-rw-r--+ 1 aime1 svrtech 83338 Apr 2 10:26 sat.log 

-rw-rw-r--+ 1 aime1 svrtech 2435 Apr 2 10:48 MAT.log

-rw-rw-r--+ 1 aime1 svrtech 3470 Apr 2 08:35 ant_build.log

I thought of using split function. How do i split on 7th space. Anyone having idea how to do that.

like image 432
cks Avatar asked Feb 21 '26 17:02

cks


1 Answers

String#split takes the regex as the arguments,so try with something like this

String data ="-rw-rw-r--+ 1 aime1 svrtech 83338 Apr 2 10:26 sat.log -rw-rw-r--+ 1 aime1 svrtech 2435 Apr 2 10:48 MAT.log -rw-rw-r--+ 1 aime1 svrtech 3470 Apr 2 08:35 ant_build.log";

    String arr[]=data.split("(?<=.log )");
    for(String s:arr){
    System.out.println(s);
    }
like image 118
Nambi Avatar answered Feb 23 '26 07:02

Nambi