7

Can you please tell me how to search in reveres order (backward search) in Java?

I need to do same for searching in HTML file as text.

aioobe
  • 413,195
  • 112
  • 811
  • 826
Ashish TheCool
  • 71
  • 1
  • 1
  • 2

2 Answers2

10

[...] how to search in reveres order (backward search) in 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 last index of the substring str. If you want to keep searching from that point, you can add a second fromIndex argument.

The exact same methods exist for StringBuilder as well.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    ... and StringBuilder has the same thing too. – Jon Skeet Jun 01 '11 at 07:19
  • I was reading this question as he's looking for an efficient implementation of searching for a string, starting from the end of the string, (say a very long html file, where you're looking for a tag you know will be in the last ~20%, and you dont want to read the first 80 for no reason...) – William Melani Jun 01 '11 at 07:23
  • Ok. I was reading it as he wanted to use String or StringBuilder, and I didn't find anything stating that it must be more efficient than the built in methods for those to classes. – aioobe Jun 01 '11 at 07:25
  • My apologies I should be been more specific. Actually I want to find a html element (specifically dropdown) by its id and then capture its XML like part (ex. . This XML like part will be usefull to run XPath query to find value attributes. So after finding id of this html element i have to search backward to find " section. thanks all for your replies. – Ashish TheCool Jun 01 '11 at 09:12
  • I would suggest you use an XML parser for this. – aioobe Jun 01 '11 at 09:17
  • i guess u mean to say HTML parser. but I can't use third party libraries because of some restrictions. – Ashish TheCool Jun 01 '11 at 10:30
-1

if want to search in file 1. Open file in random access mode. 2. go to end of file. 3. then search in string.

Kamahire
  • 2,149
  • 3
  • 21
  • 50
  • 1
    That's exactly what I want to do, but this descriptions is of literally zero help to me. Step 1 is easy, step 2 is easy, step 3 makes no sense. – mjaggard Nov 19 '14 at 10:25