I have:
/xxx/yyy/aaa/bbb/abc.xml (or)
/xxx/yyy/aaa/abc.xml (or)
/xxx/yyy/aaa/bbb/ccc/abc.xml
But I need only:
/xxx/yyyy
How do I implement this in Java? Thanks in advance.
I have:
/xxx/yyy/aaa/bbb/abc.xml (or)
/xxx/yyy/aaa/abc.xml (or)
/xxx/yyy/aaa/bbb/ccc/abc.xml
But I need only:
/xxx/yyyy
How do I implement this in Java? Thanks in advance.
 
    
    You can use StringUtils class for this.
Sample code snippet for your question,
    String str = "/xxx/yyy/aaa/bbb/abc.xml";
    int index = StringUtils.ordinalIndexOf(str , "/" , 3);
    String result = str.substring(0,index);
Or you can use indexOf method iteratively,
    String str = "/xxx/yyy/aaa/bbb/abc.xml";
    int index = 0 , count = 1;
    while(count != 3)
    {
         index = str.indexOf("/" , index+1);
         count++;
    }
    String result = str.substring(0,index);
 
    
    You can use String.split("/") for splitting and concat first 2 elements:
for(String string: input){
  String[] splitedStrings = string.split("/");
  StringBuilder result = new StringBuilder("/");
  for(int i = 1; i < 3 && i < splitedStrings.length; i++){
    result.append(splitedStrings[i]).append("/");
  }
  System.out.println("Result: " + result.toString());
}
 
    
    FilenameUtils.getFullPathNoEndSeparator(str).replaceAll("((/[^/]*){2}).*", "$1")
I used this its working fine for me.
