Re: How to check a String contains another String, case-insensitively?

Discussion in 'Java' started by Knute Johnson, Sep 11, 2008.

  1. www wrote:
    > Hi,
    >
    > I know String method contains("YES") will check if this String contains
    > "YES", but it will not find "yes", or "yEs". I want to use a method to
    > return true if it contains " YES ", " yEs ", " yes " etc.
    >
    > I have tried, using Regular Expression:
    >
    > final static String YES_CONSTANT = "YES";
    >
    > if(myStr.matches("\s*(?!)YES_CONSTANT\s*"))
    >
    > But there is some error.
    >
    > Thank you very much.


    public static int indexOfRegex(String str, String regex) {
    int retcod = -1;

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    if (m.find())
    retcod = m.start();

    return retcod;
    }

    I wrote this a while back for some discussion. Use a case insensitive
    regex:

    indexOfRegex("why yes I am","(?i:YES)");

    should return an index of 4.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Sep 11, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    929
    X-Centric
    Jun 30, 2005
  2. tomaszewski.p
    Replies:
    4
    Views:
    776
    Tom Anderson
    Sep 12, 2008
  3. Roedy Green
    Replies:
    0
    Views:
    412
    Roedy Green
    Sep 12, 2008
  4. John Deuf

    Sorting case insensitively

    John Deuf, Oct 27, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    86
    Master Web Surfer
    Oct 31, 2003
  5. PerlFAQ Server
    Replies:
    0
    Views:
    302
    PerlFAQ Server
    Feb 8, 2011
Loading...

Share This Page