[英]Java - second last occurrence of char in string
假设我有字符串
String path = "the/quick/brown/fox/jumped/over/the/lazy/dog/";
我想要以下 output
String output = "the/quick/brown/fox/jumped/over/the/lazy/";
我在想以下会做
output = path.substring(0, path.lastIndexOf("/", 1));
鉴于医生怎么说
返回指定字符第一次(最后一次)出现的索引,从指定索引向前(向后)搜索。
但这似乎不起作用。
任何帮助,将不胜感激。
似乎每个答案都假设您已经知道输入字符串以及其中最后一次出现"/"
的确切位置,而通常情况并非如此......
这是一种更通用的方法,用于获取字符串中字符的倒数第 n 个(倒数第二、倒数第三等)出现次数:
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)));
}
用法:
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
输出:
该/快速/棕色/狐狸/跳跃/超过/该/懒惰/
该/快速/棕色/狐狸/跳跃/越过/该/
/快速/棕色/
该/快速/棕色/狐狸/跳跃/超过/该/懒惰
这有效,给定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);
首先, lastIndexOf
将返回一个索引,而不是一个字符串。 它还从指定的索引1
向后搜索,因此它只会查看之前和包括索引 1 处的字符的所有内容。这意味着它只检查t
和h
。 按预期,它什么也没找到并返回 -1。
如果您想搜索整个字符串,您应该省略第二个参数。
此外,为了获得所需的输出字符串(我假设您希望删除最后一个路径组件?),您可以使用带有正则表达式的replaceAll
:
String output = path.replaceAll("[^/]+/$", "");
使用 Apache Commons IO
String output = org.apache.commons.io.FilenameUtils.getPath(path);
不使用阿帕奇
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);
}
如果您正在处理路径和文件,为什么不使用内置类? 在我看来,像下面这样的东西比字符串操作更容易:
Path path = Paths.get("the/quick/brown/fox/jumped/over/the/lazy/dog/");
System.out.println(path.getParent());
// prints: the\quick\brown\fox\jumped\over\the\lazy
System.out.println(path.getParent().getParent());
// prints: the\quick\brown\fox\jumped\over\the
例如 String key = "aaa/bbb/ccc/ddd" ;
我需要我的结果字符串为“ccc/ddd”。 即“/”的倒数第二个索引的子字符串,以下代码有帮助:
String key="aaa/bbb/ccc/ddd";
key=key.substring(key.substring(0, key.lastIndexOf("/")).lastIndexOf("/")+1);
键的最终值将是“ccc/ddd”。
这是一个用例:
String url = "http://localhost:4000/app/getPTVars";
int secondLastIndexOf = url.substring(0, url.lastIndexOf('/')).lastIndexOf('/');
System.out.println(secondLastIndexOf);
System.out.println(url.substring(secondLastIndexOf, url.length()));
和输出:
21
/app/getPTVars
尝试- 1
方法:
int j = path.lastIndexOf("/");
int i = path.lastIndexOf("/", j - 1); // the 2nd last index from the last index
String output = path.substring(0, i + 1); // inclusive
String path = "the/quick/brown/fox/jumped/over/the/lazy/dog/";
字符串 output = path.substring(0, path.lastIndexOf("/",path.lastIndexOf("/")-1)+1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.