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

    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?

    Thanks!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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:
    Regular-Expressions.info - 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"

    db

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, 01:17 PM
  2. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  3. String indexOf
    By ras_pari in forum Advanced Java
    Replies: 3
    Last Post: 10-07-2009, 08:33 AM
  4. [SOLVED] Multiple return values
    By Manfizy in forum New To Java
    Replies: 17
    Last Post: 05-25-2009, 01:07 PM
  5. Replies: 0
    Last Post: 11-15-2008, 11: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
  •