java occurrence of_Java - second last occurrence of char in string

可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:

问题:

Suppose I've the string

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

I would like the following output

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

I was thinking the following would do

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

given how the doc says

Returns the index of the first (last) occurrence of the specified character, searching forward (backward) from the specified index.

but that doesn't seem to work.

Any help would be appreciated.

回答1:

This works, given path length >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);

回答2:

First of all, lastIndexOf will return an index, not a string. It also searches backwards from the specified index 1, so it will only look at everything before and including the character at index 1. This means that it only checks t and h. Expectedly, it finds nothing and returns -1.

You should just omit the second argument if you want to search the whole string.

In addition, to achieve your desired output string (I assume you want the last path component removed?), you can use replaceAll with a regex:

String output = path.replaceAll("[^/]+/$", "");

回答3:

The lastIndexOf method's second parameter specifies the maximum index upto where the method should search the string. This means, that in your case

path.lastIndexOf("/", 1)

returns the first index of "/" whose index is smaller than 1.

回答4:

Using Apache Commons IO

String output = org.apache.commons.io.FilenameUtils.getPath(path);

Not using Apache

public static String removeLastPart(String str) {

int pos = str.length() - 1;

while (str.charAt(pos) != '/' || pos + 1 == str.length()) {

pos--;

}

return str.substring(0, pos + 1);

}

回答5:

It seems like every single answer is assuming that you already know the input string and the exact position of the last occurrence of "/" in it, when that is usually not the case...

Here's a more general method to obtain the nth-last (second-last, third-last, etc.) occurrence of a character inside a string:

static int nthLastIndexOf(int nth, String ch, String string) {

if (nth <= 0) return string.length();

return nthLastIndexOf(--nth, ch, string.substring(0, string.lastIndexOf(ch)));

}

Usage:

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

System.out.println(s.substring(0, nthLastIndexOf(2, "/", s)+1)); // substring up to 2nd last included

System.out.println(s.substring(0, nthLastIndexOf(3, "/", s)+1)); // up to 3rd last inc.

System.out.println(s.substring(0, nthLastIndexOf(7, "/", s)+1)); // 7th last inc.

System.out.println(s.substring(0, nthLastIndexOf(2, "/", s))); // 2nd last, char itself excluded

Output:

the/quick/brown/fox/jumped/over/the/lazy/

the/quick/brown/fox/jumped/over/the/

the/quick/brown/

the/quick/brown/fox/jumped/over/the/lazy

回答6:

If you are dealing with paths and files why not use the built in classes? Something like below seems to me easier than string manipulation:

Path path = Paths.get("the/quick/brown/fox/jumped/over/the/lazy/dog/");

System.out.println(path.getParent());

// prints: thequickbrownfoxjumpedoverthelazy

System.out.println(path.getParent().getParent());

// prints: thequickbrownfoxjumpedoverthe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值