如何在 Java 中使用 String 或 StringBuilder 以相反的顺序搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6197540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search in reverse order using String or StringBuilder in Java?
提问by Ashish TheCool
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 文件中作为文本搜索做同样的事情。
回答by aioobe
[...] 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)
. This will locate the lastindex of the substring str
. If you want to keep searching from that point, you can add a second fromIndex
argument.
对于此任务,您可以使用String.lastIndexOf(str)
. 这将定位子字符串的最后一个索引str
。如果您想从该点继续搜索,您可以添加第二个fromIndex
参数。
The exact same methods exist for StringBuilder
as well.
也存在完全相同的方法StringBuilder
。
回答by Kamahire
if want to search in file 1. Open file in random access mode. 2. go to end of file. 3. then search in string.
如果要在文件中搜索 1. 以随机访问模式打开文件。2. 转到文件末尾。3.然后在字符串中搜索。