How to parse this string in Java?

前端 未结 10 1376
感情败类
感情败类 2020-11-29 05:19

prefix/dir1/dir2/dir3/dir4/..

How to parse the dir1, dir2 values out of the above string in Java?

The prefix here c

10条回答
  •  -上瘾入骨i
    2020-11-29 06:09

    String str = "/usr/local/apache/resumes/dir1/dir2";
    String prefix = "/usr/local/apache/resumes/";
    
    if( str.startsWith(prefix) ) {
      str = str.substring(0, prefix.length);
      String parts[] = str.split("/");
      // dir1=parts[0];
      // dir2=parts[1];
    } else {
      // It doesn't start with your prefix
    }
    

提交回复
热议问题