자바 - 문자열에서 문자의 마지막에서 두 번째 발생

antikbd :

내가 문자열을했습니다 가정

String path = "the/quick/brown/fox/jumped/over/the/lazy/dog/";

나는 다음과 같은 출력을 싶습니다

String output = "the/quick/brown/fox/jumped/over/the/lazy/";

나는 다음을 수행 할 생각

output = path.substring(0, path.lastIndexOf("/", 1));

사이먼이 말한다 방법 제공

반환 지정된 인덱스에서 지정된 문자의 첫 번째 (마지막)에 나타나는 인덱스 전진 (후진) 검색.

그러나 그것은 작동하지 않습니다.

어떤 도움을 주시면 감사하겠습니다.

Idva :

이 작품, 주어진 path길이> 2

final String path = "the/quick/brown/fox/jumped/over/the/lazy/dog/";
final int secondLast = path.length()-2;
final String output = path.substring(0, path.lastIndexOf("/",secondLast)+1);
System.out.println(output);

추천

출처http://43.154.161.224:23101/article/api/json?id=196220&siteId=1