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

问题:

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

java occurrence of_Java - second last occurrence of char in string相关推荐

  1. [小技巧][JAVA][转换]整型int与字符char相互转换

    借助String.valueOf()过渡 char -> String ->int char c; String str = String.valueOf(c); int i = Inte ...

  2. char 转换 二进制 java_使用Java读取二进制文件并将其转换为char文件 - java

    我正在尝试读取可以在here中找到的shortcod文件二进制文件. 我用来打印此文件内容的方法: public void read3RegularGraphs( String pathFile ) ...

  3. Java char转换为String,String转换为char数组

    Today we will look into java char to String program. We will also learn how to convert String to a c ...

  4. Java中的byte[]/char[]/int/String数据类型转换

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51743969 文章出自:薛瑄的博客 你也可以查看我的其他同类文章,也会让你有一定的 ...

  5. openFOAM学习笔记(三)—— char和string相关的类

    openFOAM中的char和string并不是直接使用C++提供的string,而是进行了一系列的封装,并添加了更多的功能.这里进行一个总结. char类型的更多功能 openFOAM中并没有对ch ...

  6. Java常用类(1)--字符串相关类String、StringBuffer、StringBuilder全面知识

    文章目录 String类 StringBuffer类 StringBuilder类 String:不可变的字符序列:底层使用char[]存储 StringBuffer:可变的字符序列:线程安全的,效率 ...

  7. 夯实Java基础系列3:一文搞懂String常见面试题,从基础到实战,更有原理分析和源码解析!

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...

  8. 【shiro】UsernamePasswordToken中char[]替代String的安全性

    shiro中UsernamePasswordToken类的源码中有一段注释很有意思. * <p>Note that this class stores a password as a ch ...

  9. char*,const char*,string的相互转换 C++

    转:https://www.cnblogs.com/wxmdevelop/p/4567857.html string转const char* string s ="abc"; co ...

最新文章

  1. NDK JNI Android Studio开发与调试DEMO(三)(生成 .so 文件)
  2. ZOJ1002 Fire Net(非递归版)
  3. CS193P学习笔记(一)
  4. TF-A代码阅读: SP_EL3栈内存-cpu_data内存的介绍(cpu_context介绍)
  5. 一些让人恶心的代码片段
  6. Method Tracking
  7. 剑指Offer - 面试题10- II. 青蛙跳台阶问题
  8. 你还不知道Redis 高延迟时发生了啥嘛?
  9. Ubuntu14.04+RabbitMQ3.6.3+Golang的最佳实践
  10. 详解Python的装饰器
  11. 软件测试——第三次作业
  12. fortran 学习笔记1-编译环境
  13. php随机给用户抽奖,PHP随机按百分比抽奖
  14. scratch实现弹跳小球2
  15. 英语3500词(十二)Easter主题(2022.1.24)
  16. Qt 之 QSS(黑色炫酷)
  17. 回路电感详细介绍(环路电感)
  18. ebc是什么意思_亚马逊EBC是什么有什么用,该如何操作?
  19. HBase常见面试题
  20. blogbus博客搬家之图片迁移

热门文章

  1. linux debian 下 docker安装windows应用 qq 微信 迅雷
  2. android7玩赛尔号,赛尔号互通版
  3. Github下载加速最简单方法
  4. 第3单元 mvp架构,dagger2,butterknife的使用
  5. python修改json中的文件内容并保存
  6. win10小娜只会搜索网页(痕迹/历史清理相关)
  7. C语言函数大全--g开头的函数(下)
  8. 【内推网】错失1800万融资之后,他用900元挽救了公司
  9. vs2019下载离线安装包并离线安装
  10. (翻译)承诺与一致原理(Commitment consistency)