Results 1 to 3 of 3
  1. #1
    Arrowx7 is offline Member
    Join Date
    Aug 2008
    Rep Power

    Default How to string.indexOf for multiple strings and return first instance?

    Basically, I am searching a string containing html. And I want to find the first instance of "<input" or "<div" or "<form", and return the index of whichever comes first?
    I have a way to do it with many loops, but it's really inefficient. Is there a simple way to do it? maybe with regex?


  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power


    If all the html is in a single String then simply do index of input, div and form. Then find the min of those three values and return it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    Is there a simple way to do it? maybe with regex?
    That largely depends on how familiar you are with the classes in java.util.regex, and regex in general.

    Matcher has a method start() which returns "the start index of the previous match."

    Some learning resources for regex: - Regex Tutorial, Examples and Reference - Regexp Patterns
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    You may get better help if you describe the requirement: why you need to know the first index of "<input" or "<div" or "<form"


Similar Threads

  1. Multiple Instance of Web Application end Up Using Same JCA Resource
    By sjunejo in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 06-09-2011, 12:17 PM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. String indexOf
    By ras_pari in forum Advanced Java
    Replies: 3
    Last Post: 10-07-2009, 07:33 AM
  4. [SOLVED] Multiple return values
    By Manfizy in forum New To Java
    Replies: 17
    Last Post: 05-25-2009, 12:07 PM
  5. Replies: 0
    Last Post: 11-15-2008, 10:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts