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

Discussion in 'Java' started by tomaszewski.p, Sep 11, 2008.

  1. On 11 Wrz, 21:12, 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.


    There is one simple solution:

    public class Test {
    public static void main(String[] args) {
    final String toSearchFor = "YES";
    final String str1 = "this is yEs in string";
    final String str2 = "this is YES in string";

    System.out.println("In 1st: " +
    str1.toUpperCase().contains(toSearchFor));
    System.out.println("In 2nd: " +
    str2.toUpperCase().contains(toSearchFor));
    }
    }

    Przemek
     
    tomaszewski.p, Sep 11, 2008
    #1
    1. Advertising

  2. tomaszewski.p

    Lew Guest

    On Sep 11, 3:55 pm, "tomaszewski.p" <> wrote:
    > On 11 Wrz, 21:12, 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.

    >
    > There is one simple solution:
    >
    > public class Test {
    >     public static void main(String[] args) {
    >         final String toSearchFor = "YES";
    >         final String str1 = "this is yEs in string";
    >         final String str2 = "this is YES in string";
    >
    >         System.out.println("In 1st: " +
    > str1.toUpperCase().contains(toSearchFor));
    >         System.out.println("In 2nd: " +
    > str2.toUpperCase().contains(toSearchFor));
    >     }


    toUpperCase() and toLowerCase() are not reciprocal. In other words,
    it is not necessarily true that for a String 'foo' with all lower-case
    characters that:

    foo.toUpperCase().toLowerCase().equals( foo )
     
    Lew, Sep 11, 2008
    #2
    1. Advertising

  3. tomaszewski.p

    Tom Anderson Guest

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

    On Thu, 11 Sep 2008, Lew wrote:

    > toUpperCase() and toLowerCase() are not reciprocal. In other words, it
    > is not necessarily true that for a String 'foo' with all lower-case
    > characters that:
    >
    > foo.toUpperCase().toLowerCase().equals( foo )


    I assume this is to do with the rules in some weird writing systems (ie
    any that aren't the ones i know!). Could you expand at all on exactly when
    the equality you give might not hold?

    tom

    --
    It involves police, bailiffs, vampires and a portal to hell under a
    tower block in Hackney.
     
    Tom Anderson, Sep 12, 2008
    #3
  4. Re: How to check a String contains another String, case-insensitively?

    Lew <> wrote:
    > System.out.println(
    > "beißen".toUpperCase().toLowerCase().equals( "beißen" ));


    Sooner or later you'll have to find a new example for this
    concept. A month or so ago, I read in the newspapers,
    that a capital case version of &szlig; has been defined.
    Not sure, though, how it will go into unicode, what the
    new char is supposed to look like, and whether it will
    be tied to the lowercase version, and whether "SS" will
    continue to be a legal variant of "ß".

    PS: next candidate for an example may be the greek sigma
    which has two lowercase variants, but only one uppercase.
    (σ+ς versus Σ)

    PPS: the inequality of that round-trip is likely not so much
    a problem for the OP, than that the uppercase of one char
    might be two characters: this voids the meaning of finding
    the position of the match in the toUpperCase()d string: it
    may be different from the position in the original string.

    PPPS: if the OP accepts the limitation to plain ascii chars,
    and doesn't care about other scripts, then doing the search
    on the toUpperCase()d string is the easiest.
     
    Andreas Leitgeb, Sep 12, 2008
    #4
  5. tomaszewski.p

    Tom Anderson Guest

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

    On Fri, 12 Sep 2008, Andreas Leitgeb wrote:

    > Lew <> wrote:
    >> System.out.println(
    >> "beißen".toUpperCase().toLowerCase().equals( "beißen" ));

    >
    > Sooner or later you'll have to find a new example for this
    > concept. A month or so ago, I read in the newspapers,
    > that a capital case version of &szlig; has been defined.
    > Not sure, though, how it will go into unicode, what the
    > new char is supposed to look like, and whether it will
    > be tied to the lowercase version, and whether "SS" will
    > continue to be a legal variant of "ß".
    >
    > PS: next candidate for an example may be the greek sigma
    > which has two lowercase variants, but only one uppercase.
    > (ÿÿ+ÿÿ versus ÿÿ)


    Thanks to you both for those examples. I knew the Greek one, but had
    completely failed to realise it might apply in this situation.

    tom

    --
    Subvert the paradigm!
     
    Tom Anderson, Sep 12, 2008
    #5
    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:
    948
    X-Centric
    Jun 30, 2005
  2. Knute Johnson
    Replies:
    0
    Views:
    416
    Knute Johnson
    Sep 11, 2008
  3. Roedy Green
    Replies:
    0
    Views:
    419
    Roedy Green
    Sep 12, 2008
  4. John Deuf

    Sorting case insensitively

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

Share This Page