简体   繁体   English

如何在 Java 中使用 String 或 StringBuilder 进行逆序搜索?

[英]How to search in reverse order using String or StringBuilder in Java?

Can you please tell me how to search in reveres order (backward search) in Java?你能告诉我如何在 Java 中按崇敬顺序搜索(向后搜索)吗?

I need to do same for searching in HTML file as text.我需要在 HTML 文件中作为文本进行搜索。

[...] how to search in reveres order (backward search) in java? [...] 如何在 java 中按顺序搜索(向后搜索)?

I assume you mean search for a string starting from the end.我假设您的意思是从末尾开始搜索字符串。

For this task you can use String.lastIndexOf(str) .对于此任务,您可以使用String.lastIndexOf(str) This will locate the last index of the substring str .这将定位 substring str最后一个索引。 If you want to keep searching from that point, you can add a second fromIndex argument.如果您想从该点继续搜索,您可以添加第二个fromIndex参数。

The exact same methods exist for StringBuilder as well. StringBuilder也存在完全相同的方法。

if want to search in file 1. Open file in random access mode.如果要在文件中搜索 1. 以随机访问模式打开文件。 2. go to end of file. 2. go 到文件末尾。 3. then search in string. 3.然后在字符串中搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM